Jump to content

how to create a group with a pool of assorted enemies to pick from


pranadevil
 Share

Recommended Posts

Well, you can do something like this:

this.enemies = game.add.group();this.enemies.createMultiple(5, 'enemySpriteToUse')

That basically gives you 5 enemies to choose from.  You can look at the documentation for Groups at http://docs.phaser.io/Phaser.Group.html, and type "get" unto the search box.  It should give you a few methods such as getFirstDead, getAtIndex, etc, so you can pick out specific children from the "enemies" group.    There's also a section at http://examples.phaser.io/ that shows you how to use some of those, such as getting the first child in a group, etc.  Hope that helped.

Link to comment
Share on other sites

I'm pretty new to Phaser, but what I've done is add multiple groups in a dictionary, and then choose a random one. Something like:

var enemies = { 'A' : game.add.group(), 'B' : game.add.group(), 'C': ... }// Add enemies to the groupsfunction getRandomEnemyGroup() {    var keys = Object.keys(enemies);    var index = game.rng.integerInRange(0, keys.length - 1);    return enemies[keys[index]];}function spawnRandomEnemy() {    var group = getRandomEnemyGroup();    var enemy = group.getFirstDead();    // ... etc.}
Link to comment
Share on other sites

Well,  you could also create four separate groups of enemies, then add them to a parent group (something that I am also sort-of working with right now).  I suppose that if you did that, you could create at least one enemy in each group (say, Group1, Group2, Group3, Group4), and then get the first child object inside on of those randomly.  

 

 I'm not the best programmer in the world, and my JavaScript is seriously lacking in many areas, but this might do what you want (modified for your code, of course)?

// Create the four groups to hold your different enemy typesthis.group1 = game.add.group();this.group2 = game.add.group();this.group3 = game.add.group();this.group4 = game.add.group();// Add an enemy into each groupthis.group1.create(x, y, "enemy1");this.group2.create(x, y, "enemy2");this.group3.create(x, y, "enemy3");this.group4.create(x, y, "enemy4");// Create the parent group for all enemiesthis.enemies = game.add.group();// Add each enemy group into the parent groupthis.enemies.add(this.group1);this.enemies.add(this.group2);this.enemies.add(this.group3);this.enemies.add(this.group4);// Create a function that you can call.  Pass it the enemy group (group1, group2, etc) for whatever group you want to perform on.  Then do whatever you want inside the function.  If you have more than one enemy in each group (for example, there are 3 enemies of the type in group1, 2 of the type in group2, etc), modify the function to also accept an integer for the number of enemies you want to return.  Of course,  you'll probably want to create a list to add them to and return that, but this might be a good start for what you want?enemies.forEach(function(enemygroup){     // I'm not sure if you can check the name of the group.  If not, you can refer to the group by its index in the "enemies" group.     if (enemygroup.name == "group1")     {          // Get the first dead enemy in the group, assign it to the variable "selectedEnemy"          selectedEnemy = enemygroup.getFirstDead()          // Reset the position of the sprite for the enemy. Put it in the center of the game world.          selectedEnemy.reset(game.world.centerX, game.world.centerY)     }});
Link to comment
Share on other sites

 

Well,  you could also create four separate groups of enemies, then add them to a parent group (something that I am also sort-of working with right now).  I suppose that if you did that, you could create at least one enemy in each group (say, Group1, Group2, Group3, Group4), and then get the first child object inside on of those randomly.  

 

 I'm not the best programmer in the world, and my JavaScript is seriously lacking in many areas, but this might do what you want (modified for your code, of course)?

// Create the four groups to hold your different enemy typesthis.group1 = game.add.group();this.group2 = game.add.group();this.group3 = game.add.group();this.group4 = game.add.group();// Add an enemy into each groupthis.group1.create(x, y, "enemy1");this.group2.create(x, y, "enemy2");this.group3.create(x, y, "enemy3");this.group4.create(x, y, "enemy4");// Create the parent group for all enemiesthis.enemies = game.add.group();// Add each enemy group into the parent groupthis.enemies.add(this.group1);this.enemies.add(this.group2);this.enemies.add(this.group3);this.enemies.add(this.group4);// Create a function that you can call.  Pass it the enemy group (group1, group2, etc) for whatever group you want to perform on.  Then do whatever you want inside the function.  If you have more than one enemy in each group (for example, there are 3 enemies of the type in group1, 2 of the type in group2, etc), modify the function to also accept an integer for the number of enemies you want to return.  Of course,  you'll probably want to create a list to add them to and return that, but this might be a good start for what you want?enemies.forEach(function(enemygroup){     // I'm not sure if you can check the name of the group.  If not, you can refer to the group by its index in the "enemies" group.     if (enemygroup.name == "group1")     {          // Get the first dead enemy in the group, assign it to the variable "selectedEnemy"          selectedEnemy = enemygroup.getFirstDead()          // Reset the position of the sprite for the enemy. Put it in the center of the game world.          selectedEnemy.reset(game.world.centerX, game.world.centerY)     }});

 

 

but why when i use  item = parentgroup.getchildat(0);   got a error messge undefined is not a fuction?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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