Jump to content

remove element from group


sergil
 Share

Recommended Posts

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

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

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

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....  :wacko: 

Link to comment
Share on other sites

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

  • 2 weeks later...

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

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

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...

Link to comment
Share on other sites

thank you for these details

that'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

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 :)

Link to comment
Share on other sites

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

  • 1 year later...
  • 2 weeks later...
 Share

  • Recently Browsing   0 members

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