Jump to content

parent group index not helping!


pranadevil
 Share

Recommended Posts

:(  hi buddies,

 

 

im beginning whit this marvelous framework called PHASER.

 

 

believe me i read the docs and everything in theory works fine. but in my code something is not working.

 

lets see;

 

i've been working with groups

 

this.group1 = game.add.group();
        this.group2 = game.add.group();
        this.group1.enableBody = true;
        this.group2.enableBody = true;
        this.group1.create(0, 20, "coin");
        this.group2.create(20, 50, "enemy");
        
        ////parent group
        this.enemies = game.add.group();
        this.enemies.enableBody = true;
        this.enemies.addAt(this.group1,0);
        this.enemies.addAt(this.group2,1);
 
here the parent group contents two child group at index 0 and 1 respectively.
 
but i cant understand why when i assign the child stored in index 0, also the index 1 is been used.
 
 
this.enemies.forEach(this.dd,this);
 
 
 dd: function(enemypp)
    {
      
     
    selectedEnemy = enemypp.enemypp.getAt(0);
    selectedEnemy.body.velocity.x = 60;
        
    }
 
 
 
the output........ TWO sprites moving to the right at 60.   why???  
i understand im using a foreach and it iterartes the parent group, but is there a way to tell phaser to kindly only move the sprite which index is 0?
 
i tried using a if clause, but i dont know how to choose the first item, maybe by its name??? i dont know its syntax maybe something like if(selectedEnemy.name == "group1")....
 
 
 
help me please i want to continue using this incredible framework. :D  :D  :D  :D  :D
 
 
 
Link to comment
Share on other sites

@rich

i just want to pick one sprite from the parent group.

the main idea is to have a group from which i can pick different kinds of sprites. I saw many examples where a group is created and fullfilled with several sprites but from the same kind. Now i need a group in which i can store all the kinds of enemies for example.

Link to comment
Share on other sites

Here like this. This example will work fine with assets from the Phaser Examples repo:

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });function preload() {    game.load.image('blue', 'assets/sprites/blue_ball.png');    game.load.image('red', 'assets/sprites/orb-red.png');    game.load.image('card', 'assets/sprites/mana_card.png');    game.load.image('hotdog', 'assets/sprites/hotdog.png');}var items;function create() {    items = game.add.group();    //  Add in a bunch of different sprites    for (var i = 0; i < 10; i++)    {        items.create(game.world.randomX, game.world.randomY, 'blue');        items.create(game.world.randomX, game.world.randomY, 'red');        items.create(game.world.randomX, game.world.randomY, 'card');    }    game.input.onDown.add(pickCard, this);}function pickCard() {    //  Here we'll get the first child from the Group who's "key" value matches "card"    var card = items.iterate('key', 'card', Phaser.Group.RETURN_CHILD);    if (card !== null)    {        //  We've got a card, so let's turn it into a hotdog        card.loadTexture('hotdog');    }}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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