Jump to content

Assistance With Groups and Sprites, Please?


Nebulocity
 Share

Recommended Posts

Hello everyone, I hope the new year is treating everyone well so far!  I've been out of the loop for a while, but I'm misunderstanding something about how groups, sprites, and sprite bodies interact.  I couldn't find anything relevant on the search function, but I wanted to ask this question:  What's the commonly accepted way to create groups that contain a variety of enemy types (each type having a different sized sprite, with differing numbers of frames for animation, and different names)?

 

I have five enemy types in my game, each with differing sizes and differing animation frames (one might have 5 frames, another 8, etc).  I created a group for each of these ("enemyType1Group", and so on), add enemies to each group based on the current level, and then add each of those enemyTypeXGroups to the main "enemies" group so that I can call functions on all of it's children (in the enemyTypeXGroups) instead of having to call the same code for each group individually (like setting scale, playing animations, etc).   

 

The problem is, during my Update() function, if I call something like the following, it breaks because the properties are undefined.  I'm fairly certain I'm nesting groups incorrectly, or just going about it the wrong way, so any help would be fantastic.  Of course, if anyone has some suggestions to shape the code up into something more efficient, I'm more than open to suggestions.  Thanks!

 

createEnemies: function() {     // Removed group 4 because the sprite is being re-done.  I'll add it later.     // Create separate group for each enemy type     this.enemyType1Group = game.add.group();     this.enemyType2Group = game.add.group();      this.enemyType3Group = game.add.group();     this.enemyType5Group = game.add.group();     // Create enemies for each group based on what the current level is.     if (game.global.level == 1)          this.enemyType1Group.createMultiple(5, 'enemy1')     else if (game.global.level == 2)     {          this.enemyType1Group.createMultiple(5, 'enemy1')          this.enemyType2Group.createMultiple(5, 'enemy2')     }     else if (game.global.level == 3)     {          this.enemyType1Group.createMultiple(5, 'enemy1')          this.enemyType2Group.createMultiple(5, 'enemy2')          this.enemyType3Group.createMultiple(5, 'enemy3')     }     else if (game.global.level == 4)     {          this.enemyType1Group.createMultiple(5, 'enemy1')          this.enemyType2Group.createMultiple(5, 'enemy2')          this.enemyType3Group.createMultiple(5, 'enemy3')     }     else if (game.global.level == 5)     {          this.enemyType1Group.createMultiple(5, 'enemy1')          this.enemyType2Group.createMultiple(5, 'enemy2')          this.enemyType3Group.createMultiple(5, 'enemy3')          this.enemyType5Group.createMultiple(5, 'enemy5')     }     // Add enemy animations to each enemy group     this.enemyType1Group.callAll('animations.add', 'animations', 'right', [0, 1, 2, 3, 4, 5, 6, 7], 8, true);     this.enemyType1Group.callAll('animations.add', 'animations', 'left', [8, 9, 10, 11, 12, 13, 14, 15], 8, true);     this.enemyType2Group.callAll('animations.add', 'animations', 'right', [0, 1, 2, 3, 4, 5, 6, 7], 8, true);     this.enemyType2Group.callAll('animations.add', 'animations', 'left', [8, 9, 10, 11, 12, 13, 14, 15], 8, true);     this.enemyType3Group.callAll('animations.add', 'animations', 'right', [0, 1, 2, 3], 4, true);     this.enemyType3Group.callAll('animations.add', 'animations', 'left', [4, 5, 6, 7], 4, true);     this.enemyType5Group.callAll('animations.add', 'animations', 'right', [0, 1, 2, 3, 4, 5, 6, 7], 8, true);     this.enemyType5Group.callAll('animations.add', 'animations', 'left', [8, 9, 10, 11, 12, 13, 14, 15], 8, true);     // Group contains all enemy groups, so that I can call animations, scale, etc on all sub-groups at once.     this.enemies = game.add.group();     // Add enemy type groups to main group     this.enemies.add(this.enemyType1Group)     this.enemies.add(this.enemyType2Group)     this.enemies.add(this.enemyType3Group)     this.enemies.add(this.enemyType5Group)     // Turn on teh body physics     this.enemies.enableBody = true;     // Resize enemies if spritesheet is too large.     this.enemies.scale.setTo(0.5, 0.5);     this.enemies.setAll('body.height', (35 * .5));     this.enemies.setAll('body.width', (41 * .5));     this.nextEnemy = 0; // Set enemy spawn to now},
update: function() {     // Ensure enemies face the correct direction               this.enemies.forEachAlive(function(enemy)         {             enemy.animations.play('right');         }},

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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