Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Occasionally, you'll run into problems when linking a class to a symbol. Knowing how to resolve these issues will prevent countless headaches in the future. I've mentioned before that whenever you work with instances and Timelines and object-oriented code, you could have problems. In this file here, I've connected the TrashCan symbol to the Obstacle Base Class. Let's take a look at the Obstacle Base Class.
In Obstacle.as, the constructor method adds an EventListener to listen for the ENTER_FRAME Event. Remember, the ENTER_FRAME Event executes every frame, so over and over and over again, 24 times a second or 30 times a second, depending on how fast your frame rate is for your FLA file. So every frame, I'm running this function called checkCollision. This code may be familiar to you because you used it before.
I simply copied and pasted this code from another exercise and made very small changes to it. The only change is that I added the removeEventListener line of code. So the if statement checks to see if the boarder is hitting the can. If so, the boarder goes to the second frame and plays, and removes the can from the stage, the stage EventListener is removed for the KeyboardEvent, and again, the ENTER_FRAME Listener is removed.
Now, if I were to test the file at this point, I would find a whole slew of problems. Look at all of these problems. First of all, there's an undefined property called boarder_mc, but boarder_mc is on the Timeline, as we know. Why is there a problem here? Well, first of all, boarder_mc is not inside of the TrashCan's Timeline; it's on the main Timeline. That's why this doesn't work.
So what we can do is reference this object from the TrashCan's parent. The trashcan and the boarder have the same parent, so this should work. So we'll type MovieClip, then parentheses, parent inside the parentheses, after the parentheses, a dot. So, I'm going to check that hitTestObject in there, and then can does not exist either. Remember that the obstacle is the can, so we need to change can to this.
You'll find can in the removeChild line of code as well, and it says this. Also, the removeChild line of code is going to break as well. It's going to break because we're calling it from inside of the trashcan. It's calling it to itself, and that's not going to work. You need to have the parent object remove the child. So we can do this: parent.removeChild(this).
Now, we don't have to wrap this code inside of a MovieClip in parentheses because this is a display object container already. So that has the removeChild method. So, you don't need to wrap that in MovieClip if you don't want to. But we will need to do that for boarder_mc.gotoAndPlay. So we'll type that again, MovieClip, capital M, capital C, parentheses, parent inside the parentheses, dot after the parentheses, and then we can have boarder_mc. Now let's see what we get. Test the movie.
We have just as many errors. Now, before we test the movie, I'm going to save the file. Now I'll test the file again, and I only have one error. Flash says undefined property keyPressed. So Flash is telling me there's a problem with removing this EventListener on the main Timeline, because it doesn't recognize this keyPressed function.
It's not defined here; it's defined in the main Timeline. In this case, it may be better to simply run a function on the main Timeline from here. So let's just cut this code, and then type MovieClip, capital M, capital C, in parentheses, parent, after the parentheses a dot, and then we'll run a block of code called disable. Now we haven't created that yet. We'll create it right now.
So disable, some parentheses and a semicolon, save the file, go back to Problems.fla, click the first keyframe of the actions layer and open up the Actions panel. In here, we'll create a function called disable. So function, space, disable, some parentheses, colon, void, some curly braces, and inside of the curly braces, paste the code that you just cut. Now test the movie. We don't have any errors.
We can control the skateboarder with the keyboard. When he hits the trashcan, he falls over. Then we get this, "Cannot access a property or method of a null object reference." That's inside of the checkCollision function. So let's go there and take a look at what's wrong. See if you can find out the problem. To find it out, walk through the code, line by line, and see if you can find anything that could possibly be undefined in this code.
So we have this code. It looks all right, checking against the hitTestObject which is going to be the can, we have boarder gotoAndPlay. That's not going to be null. So there won't be a problem there. Then we have parent.removeChild(this). So after the child is removed, then this code may create a problem. So we'll just select that line of code that says parent.removeChild(this), cut the code, and then paste that at the bottom of this function as the very last thing.
So, we don't take off the child until after everything else is taken care of. So let's test the movie again after we save the file. Run into the trashcan, and we don't get any errors. So, whenever you're going to remove an object from the stage, make sure it's the last thing that you have that object do. Now, as you can see, things get a lot more complicated when you start working with class files, but it's much more rewarding. Because I've already created this Obstacle code, if I go back to the main Timeline, and let's say I want to make a different type of Obstacle, like this cat, I can put the cat in the way of the skateboarder, I can right-click the cat, choose Properties, check Export for ActionScript, set the Base Class to Obstacle and then click OK.
Flash is telling me that the Cat class doesn't exist, and it's going to create it for me. I'll click OK, test the movie, make the skateboarder run into the cat, and it's an obstacle, just like the trashcan was. So I'll close the window. So by using these object-oriented techniques, it'll take a little bit more time upfront, but in the long run, you'll save large amounts of time in building your applications.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101810 Viewers
61 Video lessons · 88565 Viewers
71 Video lessons · 72380 Viewers
56 Video lessons · 104085 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.