ritzbitz01 Posted July 4, 2014 Share Posted July 4, 2014 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 More sharing options...
rich Posted July 4, 2014 Share Posted July 4, 2014 Yes, that's intended behaviour. Groups are display list level containers, not object data level, and a Sprite cannot exist in two containers at once (well it can via nesting, but it only has 1 actual parent). Link to comment Share on other sites More sharing options...
Elgan Posted July 4, 2014 Share Posted July 4, 2014 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, etcThere must be a JS container , even though it's very easy to use arrays ;D Link to comment Share on other sites More sharing options...
ritzbitz01 Posted July 4, 2014 Author Share Posted July 4, 2014 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 More sharing options...
bazoo Posted July 5, 2014 Share Posted July 5, 2014 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: 1FIRST GROUP LENGTH AFTER ADDING TO SECOND GROUP: 1 Link to comment Share on other sites More sharing options...
Recommended Posts