Jump to content

set properties of group members


Recommended Posts

I got a game class and various enemy classes.

e.g. a class 

BasicGame.Enemy_1 = function(){  this.name = "Enemy_1";  this.hp = 10;};



BasicGame.Enemy_2 = function(){  this.name = "Enemy_2";  this.hp = 330;};

in both classes sprite/image properties are also set for a game sprite to be created from these informations


now in my game i want to collision detect my player and instances of the above enemies (1-n)

for this i think i need a "group" of enemies, which can be achieved with

this.enemies = game.add.group();

so that i don't have to check each enemy instance collision on it's own.

afterwards i'd like to add various enemy instances to this group and display them.


the problem is, that only sprite/display objects could be added to groups.

but when i only add the sprites, all my properties, like hitpoints, damage, aso. cant get read from the sprite itself.

i thought that i could write a helper function to add all needed properties from the class to the sprite, but i didn't find a method "addProperty" for sprites (like there is one for groups - "setProperty")


what's the best way to set the needed properties, which could be read from colliding objects aso.?


Link to comment
Share on other sites

Adding a property is easy as:

this.existingEnemySprite.property = 20;this.existingEnemySprite.name = "enemy01";

Collision with group goes:

game.physics.arcade.collide( this.player, this.yourGroup, this.collisionFunc);

and your function for different enemyTypes:

collisionFunc: function( _player, _enemy){    if (_enemy.name = "enemy01"){        // ... do for enemy 01    }}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...