Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by quidmonkey

In collideWith() or check() it would be helpful to know the Object type or name of the other parameter. I'd rather not have to loop through a switch() of instanceof's for all Entity types I have defined. Any ideas?

1 decade ago by MikeL

Let's say a bomb entity is checking against a glider entity. You can simply give EntityGlider a name property as in:

name: "glider",
...

Then in the check function of the bomb, just see if the name matches:
if (other.name == "glider"){
  //There is a match so do whatever needs to be done here
}

1 decade ago by fugufish

@MikeL -> can we assign the name via Weltmeister? (i.e we select the entity via a mouse click, and add the "name" property )

1 decade ago by dominic

@fugufish: yes, you can assign the name property in WM.

Back to the original topic: what would be the benefit of
if( other.name == "glider" ) { ... }

over
if( other instanceof EntityGlider ) { ... }

?

1 decade ago by MikeL

Well the original statement was phrased "it would be helpful to know the Object type or name of the other parameter". Which I took to mean "how can I introspectively retrieve the name or type of the entity?"

I can't for example do something like:
alert('instance ' + EntityGlider.instance);

so far as I know, to directly retrieve the string value of the EntityGlider name. You can check if it&039;s an #instanceof which is probably suitable for most cases as you are indicating. I came across this issue when I was trying to get the string value of javascript object names and that was the only workaround I could find.

But is there another way?

1 decade ago by dominic

Well, there's no way in JavaScript to get the name of a class (or property for that matter) directly, without having the name stored as a string somewhere. The reason is quite simply that there's no unambiguous "name" for anything:
FooClass = ig.Class.extend({});
BarClass = FooClass;

foo = new FooClass();
foo instanceof FooClass; // true
foo instanceof BarClass; // true

// fictional:
foo.getClassName(); // Which one is it? 'FooClass' or 'BarClass'?

But you could overwrite the spawnEntity() method in your game class (untested):
spawnEntity: function( type, x, y, settings ) {
	var ent = this.parent( type, x, y, settings );
	ent.className = typeof(type) === 'string'
		? type
		: 'unknown';
		
	return ent;
},

After that, all entities will have a .className property. However, you have to restrict yourself to always call spawEntity() with the class name as string, instead of with the class itself. E.g.:
var g1 = ig.game.spawnEntity( 'EntityGlider', x, y );
g1.className; // EntityGlider

var g2 = ig.game.spawnEntity( EntityGlider, x, y );
g2.className; // unknown

1 decade ago by MikeL

Ah, ok thanks for clearing that up. That's very useful info.
Page 1 of 1
« first « previous next › last »