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 Imran

Hey guys!

I'm pretty new to programming in general, and I'm trying to do something here, let me explain:

I'm trying to modify the receiveDamage(); function on a per entity basis so that if Enemy A receives damage it does one thing while if Enemy B receives damage it does another thing. So far, I've done this by just adding my own receiveDamage function into every new enemy entity, but I was wondering - is there another way to do this that isn't so copy/paste heavy? I still want the core of receiveDamage to remain (lose health, check to see if entity should be killed).

Thanks guys!

1 decade ago by dominic

I'm not sure I understand you correctly, but it sounds like you have different types of entities, where some share the same behavior, right?

Say you have an EntityPlayer that uses one version of receiveDamage() and EntityShark, EntityOctopus and EntityPirate that use another version of receiveDamage().

If that&039;s somewhat similar to your situation, you could create base classes for each of those two behaviors. I.e. create an #EntityFriend and EntityEnemy class, and subclass the player and the enemies from those two, instead of directly from ig.Entity. The two versions of your receiveDamage() function would then be in EntityFriend and EntityEnemy.

You'd end up with the following class hierarchy:
ig.Entity
	EntityFriend
		EntityPlayer
	EntityEnemy
		EntityShark
		EntityOctopus
		EntityPirate

I hope that makes sense :)

Edit: or I completely misunderstood you and you just wanted to know about this.parent(). E.g.:

receiveDamage: function( amount, from ) {
	/*
		do your own stuff here
	*/
	
	
	// call the parent receiveDamage() method from ig.Entity
	this.parent( amount, from );
}

1 decade ago by Imran

This is why I paid for Impact, dominic thank you so much for the support, the first thing you mentioned is exactly what I was looking for!

Keep up the great work :)
Page 1 of 1
« first « previous next › last »