kestorr Posted August 11, 2014 Share Posted August 11, 2014 Hello there. Im new to phaser so im trying to learn it, but in the process i have came across a few troubles with groups and forEach method. Here is what i have:starGroup.forEach(function(eachStar) { if (gameBegun==true && planeSprite.body.hitTest(eachStar.x+8, eachStar.y+30) && eachStar.frame<=30) { eachStar.animations.play('taken', 24, false, true); } if(eachStar.frame>50)// this line here doesnt work for some reason { alert('easf'); }});So if you notice that if(eachStar.frame>50), it doesnt work, for some strange reason it doesnt work.It works if i do frame<50. So its really odd.< works> doesnt work. I also have a problem if i say eachStar.destroy(), i also seted it to true like so: eachStar.destroy(true) but it gives me the following error: "Type Error: eachStar is undefined."eachStar.kill() works but i want to completely remove eachStar under certain circumstances. So whats causing such problems? Thanks. Link to comment Share on other sites More sharing options...
rich Posted August 11, 2014 Share Posted August 11, 2014 Obvious one first - do you actually have any sprites with a frame set > 50? Easy enough to test - console.log out eachStar.frame inside your function and see what comes out. Link to comment Share on other sites More sharing options...
Dumtard Posted August 11, 2014 Share Posted August 11, 2014 For second part: http://www.html5gamedevs.com/topic/8430-destroy-specific-item-from-group/Basically don't increment when destroying in a loop, if you do it will cause your loop to skip an index and go 1 too many iterations, hence undefined. Link to comment Share on other sites More sharing options...
kestorr Posted August 12, 2014 Author Share Posted August 12, 2014 Obvious one first - do you actually have any sprites with a frame set > 50? Easy enough to test - console.log out eachStar.frame inside your function and see what comes out. Yeah the console log for eachStar.frame is stuck at 0 but not sure why. Maybe im calling the wrong thing. What i want is currentFrame so when an aniamtion is at a certain frame to do a certain thing. When i made my group animations, it was like this: starGroup.callAll('animations.add', 'animations', 'idle', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30], 24, true);starGroup.callAll('animations.add', 'animations', 'taken', [31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51], 24, true);starGroup.callAll('animations.play', 'animations', 'idle', 24, true); So i thought it plays those frames, but it seems when it plays idle its stuck at frame 0. But the animation "idle" plays. For the second problem, so then how can i remove (i mean destroy, not kill) a certain sprite from that group when a certain thing happens? Thanks a bunch for the replies. Link to comment Share on other sites More sharing options...
lewster32 Posted August 12, 2014 Share Posted August 12, 2014 Killing a sprite will be fine in the majority of cases as it stops rendering and calculating physics, so unless you're spawning thousands of sprites constantly like in a particle system, this won't be a concern. You should really be pooling your objects like the bullets in this example rather than creating and destroying mid-game: http://gamemechanicexplorer.com/#bullets-2 Link to comment Share on other sites More sharing options...
kestorr Posted August 12, 2014 Author Share Posted August 12, 2014 Killing a sprite will be fine in the majority of cases as it stops rendering and calculating physics, so unless you're spawning thousands of sprites constantly like in a particle system, this won't be a concern. You should really be pooling your objects like the bullets in this example rather than creating and destroying mid-game: http://gamemechanicexplorer.com/#bullets-2 Thanks for the reply. So you are saying that if i kill all the sprites like: level buttons, backgrounds, all levels stuff and then add others (for example like hiting retry), it wont cause any performance problem throughout the game?What about the lesser mobile devices? Link to comment Share on other sites More sharing options...
lewster32 Posted August 12, 2014 Share Posted August 12, 2014 Kill them and revive them when you want them back. Sprites, buttons and so on are all just data representations which take up comparatively minuscule amounts of memory - the assets such as images they use for textures (which take up the majority of the memory in any app) are just referenced, so the difference between 1 and 10,000 sprites is tiny memory-wise, when compared to adding another image for instance. It's rendering performance which would make a difference if you tried to display 10,000 sprites, but if 9,999 of them were dead, the performance would be pretty much the same as if you only had a single sprite. By pooling your objects and reviving them when they're needed, you ensure the memory footprint remains consistent. If you start creating and deleting/destroying lots things in-game you'll get the garbage collector stepping in every so often and that tends to create noticeable 'hitches' while it cleans up all the deleted objects. Link to comment Share on other sites More sharing options...
kestorr Posted August 12, 2014 Author Share Posted August 12, 2014 Managed to fix my problem with frame also.Instead of eachStar.frame i used eachStar.animations.currentFrame.indexBut also had to use another if(eachStar!=undefined) since it was giving an undefined error after an itteration when i killed that sprite. Link to comment Share on other sites More sharing options...
Recommended Posts