Learning By Doing Posted March 5, 2014 Share Posted March 5, 2014 Hello everyone! Im pretty new to all this programming, and I been playing around with Phaser for about 3 weeks, I have made a very simple game with the help of all your great tutorials, and this awesome forum! When I run the game on my iPhone it crashes my phone sometimes, and the same use to happend for my mozilla web browser.. I belive its becase of some memory leak maybe?So my question is.. What and how do I destroy things in my code? I guess I should destroy something from my code in my quit_game function? quit_game: function () { // Start the 'MainMenu' state. this.game.state.start('MainMenu');}, Some examples of destroying items would be very nice. Link to comment Share on other sites More sharing options...
Heppell08 Posted March 5, 2014 Share Posted March 5, 2014 You probably want to look at either your update or the amount you are trying to create. It really could be anything but having very little code example makes it hard to diagnose.Post some more code or elaborate and when it crashes or how it crashes etc.As for destroying something you would call object1.destroy();but destroying something should only be done under certain circumstances. Like clearing a map on next level or re-using something. If you were to use destroy on a player or enemy it would mean having to totally re-create again from scratch in code. Link to comment Share on other sites More sharing options...
BadBearGames Posted March 5, 2014 Share Posted March 5, 2014 I usually have a shutdown function that I call when leaving a state (not sure if this is the best thing to do every time, I'm a newbie myself). Here's an example:function shutdown (game) { if (game.playButton) { game.playButton.destroy(); game.playButton = null; } if (game.muteButton) { game.muteButton.destroy(); game.muteButton = null; } if (game.fullscreenButton) { game.fullscreenButton.destroy(); game.fullscreenButton = null; } if (logo_mc) { logo_mc.destroy(); logo_mc = null; } if (backRot) { backRot.destroy(); backRot = null; } if (select_snd) { select_snd = null; }}I destroy everything in that state and set it to null, just to be safe. And yeah, it's hard to diagnose the problem without some more insight. When I first started on mobile, my images were too big, which would crash my game. Could be any number of issues. Link to comment Share on other sites More sharing options...
Heppell08 Posted March 5, 2014 Share Posted March 5, 2014 You don't need to set them to null after destroy() because they don't exist anymore in the game after destroy().Are you destroying because your going from eg: menu to game or from level 1 to level 2?The fact that you want to clean up is fine and makes sense but it depends why and under what circumstances you want to destroy.Does the crash happen on that shutdown function? Before the function? Link to comment Share on other sites More sharing options...
Recommended Posts