joshby Posted August 15, 2014 Share Posted August 15, 2014 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 More sharing options...
lewster32 Posted August 15, 2014 Share Posted August 15, 2014 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 More sharing options...
joshby Posted August 15, 2014 Author Share Posted August 15, 2014 Makes sense now. Thanks for the quick response! lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts