ageibert Posted November 19, 2014 Share Posted November 19, 2014 I got a game class and various enemy classes.e.g. a class BasicGame.Enemy_1 = function(){ this.name = "Enemy_1"; this.hp = 10;};and 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 withthis.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 More sharing options...
Sam Posted November 20, 2014 Share Posted November 20, 2014 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 More sharing options...
rvizcaino Posted November 20, 2014 Share Posted November 20, 2014 A useful function is setAll, setAll(key, value, checkAlive, checkVisible, operation, force)it sets properties of all children of group even it they do not exists, take a look at http://docs.phaser.io/Phaser.Group.html#setAll) Link to comment Share on other sites More sharing options...
ageibert Posted November 24, 2014 Author Share Posted November 24, 2014 Than you both for your answers. This helped me a lot Link to comment Share on other sites More sharing options...
Recommended Posts