Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So now we have our basic game play completed. So we have our collisions happening and we determined when we're going to go to the GameOver state. But now we need to clean up after ourselves in the play state. So we need to implement the destroy methods. So first, let's go to our BulletManager. So in our Manager classes, we need to do a couple of things in the destroy method. First, we're going to destroy the pool. So to do that, we're going to call the destroy method of the pool. Then we're going to set pool = null and we're also going to set our array to null.
So our array here is called bullets, and we'll set it equal to null. When it comes to cleaning up after yourself and garbage collection in Flash, it's better to do too much than not enough. So it's somewhat of a mystery sometimes about how to force things to be reclaimed in memory, so it's better just to go overboard and just be sure that you're cleaning up after yourself. So we're going to copy this because we're going to do a similar thing in the AlienManager class. I am going to come down to the destroy method, paste that in, and obviously our array here is called aliens.
Now we want to go to our play method, and implement the destroy method here. Now what I'm going to first do is to copy these two event handlers where we listen for the mouse up and mouse down, and let's go ahead and make sure that those are actually unregistered. So we'll do that first. We'll change that to removeEventListener. Now we're going to call those destroy methods on the BulletManager and the AlienManager. So bulletManager.destroy(); and then alienManager.destroy(), and then lastly let's remove this play state from the display list of our game.
So we're going to say removeFromParent and we're going to pass in (true) to dispose it. Now let's go ahead and test it, so I'm going to debug. We'll go to our actual game. So everything is working and now when I die, we actually remove the play state and now here is the GameOver state. Obviously, we haven't done anything yet in the GameOver state, but we'll be doing that in the next chapter.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104921 Viewers
56 Video lessons · 116780 Viewers
71 Video lessons · 85985 Viewers
131 Video lessons · 41122 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.