Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you destroy an enemy object in the game or click to close a menu in one of your apps, you may need to free those objects from memory to increase performance. While you can't control which objects Flash deletes from memory, you can control which objects are eligible for garbage collection. Garbage collection refers to when Flash incrementally cleans up memory. When your movie begins to take up too much memory, now this 'too much' is not really defined by Flash or Adobe, Flash will do something called garbage collection, and take objects that are no longer being used and throw them out, deleting them from memory.
So in order to make sure that objects get deleted when Flash does garbage collection, you need to make sure the objects are eligible for garbage collection. So on this Stage here I have boarder_mc and a movie clip called can. Now, boarder_mc has an animation that starts on frame 2, and the frame 1 is stopped. Let's go to the first keyframe in the actions layer and open up the Actions panel. So the boarder can be controlled with the keyboard, and in that keyboard block of code, I checked to see if the boarder is touching the can.
That's a method called hitTestObject, so boarder_mc.hitTestObject, capital T, capital O, and in parentheses (can). So if the boarder is touching the can, then run the block of code in curly braces, which will play at the second frame of the boarder movie clip. Let's see how that looks. So I'll move the boarder to the right. When it hits the trashcan, I'll release my finger off the keyboard and see him animate downward.
Let's say you wanted to remove the trashcan when he hits it. Go back to the code, and below that line of code, type removeChild, can, in parentheses. Make sure child has a capital C, and that can is spelt just like it is in the instance name on the Stage. If you test the movie now, you may see errors. So, the animation happens, but in the Output window, you'll see that there is something wrong.
Everything says that a supply DisplayObject must be a child of the caller. Now, what that's referring to is the removeChild call. Once we take the can out of the display list, we can no longer remove it from the display list. So we've got to make sure this code stops running once it executes the first time. To do that, I'll go in to tell the Stage to stop listening for keyboard events. So, we'll scroll up in my code, copy line of code where I add the EventListener to the Stage - that's Command+C or Ctrl+C, and then right below removeChild(can), I'm going to paste the code I just copied that says addEventListener, and change the word 'add' to 'remove.' That will stop the Stage from listening to keyboard events, and responding to them.
So when the boarder hits the can, the can will disappear, the skateboarder will fall over, and the keyboard interactivity will be removed. That should not give us any more errors. So let's test the movie, drive over to the trash can, and the skateboarder falls over. So in order to make an object eligible for garbage collection, you'll need to take it out of the display list, which you can do by using the removeChild method. In addition, you'll have to make sure that any code that references that object, like the code I have here and here, is no longer executed, by removing the appropriate event listeners.
You also have to make sure there are no event listeners attached to that object. Finally, make sure that object is taken out of any arrays that it may be in. If you do all these steps, your object will be eligible for garbage collection and will be deleted when that happens. That way, your applications can be optimized.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92889 Viewers
82 Video lessons · 104248 Viewers
71 Video lessons · 75970 Viewers
56 Video lessons · 107324 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.
Your file was successfully uploaded.