Adding multiple entities from the same class


(Oskars_Rullis) #1

Hello. I am happy to see FlashPunk community to be up and running again. :smile:

For the past few weeks I am slowly working on a platformer type game with brawl elements, and for that i need to have multiple entities, from the same class. My problem is that i can’t get my head around of how to create multiple entities, using the same class, but so that i can access each and one of those entities (in this case Enemy entities). So for example - when Player attacks enemy (collision && Input.check) it calls function Enemy.receivedmg();. But that means that it kills all of the enemy entities on the screen, not just the one i was colliding with.

I did check out the “Accessing entities” tutorial, but i still didn’t get it how it should be done. Should i create an array and somehow check with which of those enemies from the array is the player colliding with? Or are there some general guidelines of how this is being done?


(Jacob Albano) #2

It sounds like you’re calling a static recieveddmg() funtion on your Enemy class. That’s not good programming design because it only works with a single enemy instance.

The collision tutorial here has an example of how you can tell which instance the player is colliding with.

var e:Enemy = collide("enemy", x, y) as Enemy;

if (e != null)
{
    e.damage();
}

(Oskars_Rullis) #3

I wrote a long text saying that i am still quite confused - why it does not work, but then i realised, that some of the public variables and booleans were declared public static. After making them just public (after re-reading what you said) everything started functioning as it should be.

So in the end the problem wasn’t actually in the collision detection itself (that’s basically the same code i already had), but in the way i am declaring variables. Thank you for help and the really quick response! :smile:


(Jacob Albano) #4

No problem! Static variables got me in trouble quite a number of times when I was first getting started programming, so I make it a point to dissuade people from using them whenever possible. They do have their uses sometimes, but more often they cause problems like this where they break everything despite the rest of the code being fine. Glad I could help!


(fedyfausto) #5

whereaver you can put Entity into an entity, for example you have a Player Entity and you wanna add a SwordAttack Entity for another collision with enemy (Player -> Enemy = Player . damage, Swordattack->Enemy=Enemy. damage) so when you create a layer, into PLayer add a new Sword attack entity with this.world.add(myattackentity), set x,y of attack same of player and update this in player or swordattack entity. now if you press x button control in Sword attack launch the attack animation and check the collision with an enemy, remember to flip the attack entity when the player flip :slight_smile: