Jump to content

Sprite can't exist in two groups?


ritzbitz01
 Share

Recommended Posts

In my game I am tracking all player ships as well as all selected ships, which will be a subset of all player ships.  I add the ship to the allShips list, then add it to the selectedShip list when selected.  After adding it to the second group it seems to be removed from the first group.  Is this intended behavior?
 
Example:

function create() {    game.physics.startSystem(Phaser.Physics.ARCADE);    game.world.setBounds(0, 0, 800, 600);    this.game.stage.backgroundColor = '#71c5cf';    firstgroup = game.add.group();    secondgroup = game.add.group();    // Add the test fighter    testFighter = game.add.sprite(400,200, 'yellowship');    firstgroup.add(testFighter);    console.log('FIRST GROUP LENGTH BEFORE ADDING TO SECOND GROUP: ' + firstgroup.length);    secondgroup.add(testFighter);    console.log('FIRST GROUP LENGTH AFTER ADDING TO SECOND GROUP: ' + firstgroup.length);}

Console output:

 

"FIRST GROUP LENGTH BEFORE ADDING TO SECOND GROUP: 1"
"FIRST GROUP LENGTH AFTER ADDING TO SECOND GROUP: 0"
 
Thanks!
Link to comment
Share on other sites

i like groups, like OP, i nearly forgot about arrays! use them,

I think a container like groups thats actually an array with useful members, would be useful, 

member-add, remove, etc

There must be a JS container , even though it's very easy to use arrays ;D

Link to comment
Share on other sites

This is my first time using javascript, so still learning.  Arrays are super easy to use, and that is what I will use for the selected group.  Was hoping to take advantage of everything the group construct provided, but that's ok, arrays are pretty versatile as well!

 

Thanks!

Link to comment
Share on other sites

This works for me:
 

function create() {    game.physics.startSystem(Phaser.Physics.ARCADE);    game.world.setBounds(0, 0, 800, 600);    this.game.stage.backgroundColor = '#71c5cf';        // First group    firstgroup = game.add.group();    firstgroup.createMultiple(1, 'yellowship');        // Display sprite from First Group    fgSprite = firstgroup.getFirstExists(false);    fgSprite.reset(400, 200);        // Console test #1    console.log(        'FIRST GROUP LENGTH BEFORE ADDING TO SECOND GROUP: ' + firstgroup.length    );                        // Second group    secondgroup = game.add.group();    secondgroup.createMultiple(1, 'yellowship');                // Display sprite from Second Group    sgSprite = secondgroup.getFirstExists(false);    sgSprite.reset(400, 200);        // Console test #2    console.log(        'FIRST GROUP LENGTH AFTER ADDING TO SECOND GROUP: ' + firstgroup.length    );}

Console output:

 

FIRST GROUP LENGTH BEFORE ADDING TO SECOND GROUP: 1

FIRST GROUP LENGTH AFTER ADDING TO SECOND GROUP: 1

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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