sergil Posted November 6, 2013 Share Posted November 6, 2013 Removing an element from a group will destroy the element? is necessary to destroy the element to? how can I destroy a bitmap font? Link to comment Share on other sites More sharing options...
rich Posted November 6, 2013 Share Posted November 6, 2013 If you call destroy() on it, then yes it will destroy it as best it can. If you want to just recycle it for later use, then call kill() on it instead (and 'revive' it later). Link to comment Share on other sites More sharing options...
sergil Posted November 6, 2013 Author Share Posted November 6, 2013 My problem with this is that I have 3 sprites and 3 bitmaps fonts objects that change every level... so when I complete a level I show a popup with the score and if I press the continue button I would like to change the 3 sprites and bitmaps text because the goals have changed... I used destroy for the objects and bitmap fonts, but for bitmap fonts it throws the error : 'Uncaught TypeError: Cannot read property 'parentNode' of undefined ' doing some tests I found that removing the objects from groups eliminate the objects from screen and I think It was because the objects were destroyed... Link to comment Share on other sites More sharing options...
rich Posted November 7, 2013 Share Posted November 7, 2013 Try not to destroy objects if you can help it, try to re-use them, especially something like a BitmapText. However you should still be able to destroy a BitmapText, but you're right it can throw an error in some cases, so I've added in an extra check that should avoid this. Link to comment Share on other sites More sharing options...
sergil Posted November 7, 2013 Author Share Posted November 7, 2013 if you do twice: newsprite = this.game.add.sprite(X, Y, 'sprite');newsprite = this.game.add.sprite(X, Y, 'sprite2'); it creates two sprites... I was searching the way to change the sprite of newsprite variable the easy way from 'sprite' to 'sprite2' Thanks for the BitmapText revision Thanks for all the answers!!! Link to comment Share on other sites More sharing options...
rich Posted November 7, 2013 Share Posted November 7, 2013 Do you just want to change the appearance of the sprite? I.e. swap its texture? Link to comment Share on other sites More sharing options...
sergil Posted November 7, 2013 Author Share Posted November 7, 2013 I think I don't want to change the appearance.. I want to change the entire sprite with other. the problem is that I have a fixed sprite in my user interface that can be different related to the life of the player I.e. a sprite that has a smile face and when life is lowering in the game, it changes to a sad face Sorry for my poor english.... Link to comment Share on other sites More sharing options...
beeglebug Posted November 7, 2013 Share Posted November 7, 2013 If I were you I would use a spritesheet. Create a single sprite with all the faces in a row, then add it as a spritesheet, and create a single frame animation for each face. Then you can just switch animations to change the face without having to worry about destroying, hiding or any of that stuff. Link to comment Share on other sites More sharing options...
sergil Posted November 11, 2013 Author Share Posted November 11, 2013 I was thinking on doing this, but I was investigating if a sprite could be changed by other thanks! Link to comment Share on other sites More sharing options...
jerome Posted November 26, 2013 Share Posted November 26, 2013 Hi,I'm new with this great framework. I'm currently learning from the examples as the space invader game : http://gametest.mobi/phaser/examples/_site/view_full.html?d=games&f=invaders.js&t=invaders On calling the collisionHandler(), objects "bullet" and "alien" are killed, so they are removed from their respective group (group "aliens" for ... the aliens) but stay "recyclable" for further use (GC optimisation, I guess ?). But destroyed aliens seem to continue to fire after destruction. Are "killed" objects from a group still candidate to the Group.getRandom() method ?If yes, is the group re-sorted at each kill() call so the "active" objects could be set at the first indexes in the group and the inactive objects afterwards ?or a mean to declare to one group the killed object among its members ? Or, briefly, is there another way to invoke methods only on active (not yet "killed") objects in a group ? thank you Link to comment Share on other sites More sharing options...
hackenstein Posted November 26, 2013 Share Posted November 26, 2013 You are right, it looks like getRandom does not check if the returned child is alive. You could simply randomly draw children until you found one that is alive (or until some threshold is met, so you won't loop indefinitely). Take a look at src/core/Group.js for all available methods. Link to comment Share on other sites More sharing options...
jerome Posted November 29, 2013 Share Posted November 29, 2013 ok, maybe I should use something like :aliens.sort('exists'); to sort the group according to the 'exists' member of the sprite (if booleans can be sorted this way)then, assuming the existing sprites are now at the first indexes :var shooter = aliens.getRandom(0, aliens.getIndex(aliens.getFirstExists(false)));I'll test it asap Link to comment Share on other sites More sharing options...
Alvin Posted November 29, 2013 Share Posted November 29, 2013 The issue in the invaders example has been fixed, have fun http://gametest.mobi/phaser/examples/_site/view_full.html?d=games&f=invaders.js&t=invaders jerome 1 Link to comment Share on other sites More sharing options...
jerome Posted November 30, 2013 Share Posted November 30, 2013 Nice fix :-) thanks Don't you think, regarding the GC behavior, that creating a new array of livingEnemies each time we call enemyFires() will decrease the performances on some slow devices or platforms ? Link to comment Share on other sites More sharing options...
jcs Posted November 30, 2013 Share Posted November 30, 2013 yes, it will. not just on some platforms, it will decrease performance on all platforms. it just won't be significant except perhaps on very slow ones (though probably any platform that slow won't run the game at an acceptable frame-rate anyway). but it's not so much creating the array that one should be concerned about - that's a fixed cost at a known place in the game's execution - it's the garbage collection of the object that is troublesome. the garbage collector won't run at a predictable time, and the more allocations it has to free the longer it will take to complete. when it gets bad enough you'll notice a nasty "saw tooth" effect on your frame-rate. allocations should be kept out of the update loop as much as possible (completely is best). in this case the array could be moved to a global and cleared in 'enemyFires()' instead of allocated. not very important in this little game, but as an example it does act somewhat as a "style guide" to those who might not know otherwise... jerome 1 Link to comment Share on other sites More sharing options...
jerome Posted November 30, 2013 Share Posted November 30, 2013 thank you for these detailsthat's exactly what I meant when I wrote about 'creating an array' but my English is not that good to express things as what I wish :-p so in general for javascript game development, do you advise to declare the majority of variables in the global scope ? Link to comment Share on other sites More sharing options...
jcs Posted November 30, 2013 Share Posted November 30, 2013 not global necessarily, no, but not local inside functions called by the game loop. they can be stored as properties on objects that are persistent throughout the game or the current State, or can be captured in closures which are persistent throughout the game or current State. but creating objects as local variables inside functions that are called by the game loop (State.update() for instance) will cause them to be created every loop and cause GC to happen more frequently and/or for longer (as you suspected). for instance, Phaser objects have lots of "private" properties (names with leading underscores are indicative of variables that should be considered private) which it uses to avoid allocating objects during the game loop. take a look at the Phaser source code, it does a good job at avoiding allocations inside the loop. keeping things as globals is easy, but isn't good practice. sooner or later it will come back to bite you. better to create one global object for your game and store all of your objects, functions etc inside of that object. then you only have one name to worry about conflicting with. hope that helps jerome 1 Link to comment Share on other sites More sharing options...
jerome Posted December 1, 2013 Share Posted December 1, 2013 thanks a lot for all those good advices and good practices !!! shawnbless 1 Link to comment Share on other sites More sharing options...
jerome Posted December 2, 2013 Share Posted December 2, 2013 As I twisted my knee and have to stay on my bed, I spend much time reading the posts of this forum. This is how I discovered that the order of objects in a group is the order used for rendering them. So my previous proposal to sort the group 'aliens' on the value of the 'exists' member of its contained objects wasn't maybe such a good idea ...Perhaps, it has no impact on this small game, but I think sorting a group for other reasons than rendering concerns is not a good practice.So please forget my code example. Link to comment Share on other sites More sharing options...
AJAYTV90 Posted March 20, 2015 Share Posted March 20, 2015 Hi, Anyone can help me?How to kill all the sprites from a group in phaser?? Link to comment Share on other sites More sharing options...
BunBunBun Posted April 2, 2015 Share Posted April 2, 2015 AJAYTV90, use groupName.removeAll(); Link to comment Share on other sites More sharing options...
Akshar Patel Posted April 2, 2015 Share Posted April 2, 2015 Also, you can do the following (If you only want to kill the sprite and not destroy it) : groupName.forEach(function(member) { member.kill();}); Link to comment Share on other sites More sharing options...
rich Posted April 2, 2015 Share Posted April 2, 2015 Or even:group.callAll('kill'); Akshar Patel 1 Link to comment Share on other sites More sharing options...
Recommended Posts