Jump to content

Adding sprite in update function


joshby
 Share

Recommended Posts

When I add a sprite to my game in the update function, the sprite is displayed, but the kill() method doesn't work. I suspect this is because the game property of the sprite is set to null. If I add the sprite in the create function instead, the game property is set correctly and kill() works just fine.

 

Should I not be adding sprites in the update function? If I need to add all my sprites in create, what's the recommended way of keeping them hidden until I want them to be displayed?

Link to comment
Share on other sites

If you're adding the sprite in your update function, you're actually adding it as much as 60 times per second, so kill will only get rid of the most recent one. You should create sprites in the create method, or places which you can guarantee are only going to be called once. If you want to create one or several sprites in the background and only reveal them when needed, you can use 'pooling' like this example's bullets: http://gamemechanicexplorer.com/#bullets-2

 

Basically, you .kill() the sprite as soon as you create it, and then call .revive() on it later when you want it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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