Viewers: in countries Watching now:
In ActionScript 3.0 in Flash Professional CS5 Essential Training, Todd Perkins shows Flash designers how to incorporate ActionScript code into their projects and create interactive presentations and applications. The course includes a review of ActionScript language basics and the object-oriented programming (OOP) methodology, a tour of those Flash Professional CS5 features designed for developers, such as code hinting and the Code Snippets panel, and instructions on interacting with objects in the Library and placing code on the Timeline. Exercise files are included with the course.
Your knowledge of the display list can be used to access any object in Flash, from virtually any location. One example of that is the parent property, which allows you to communicate backwards from one object to its parent display object container. On the Stage here, I have boarder_mc, which is an animation. If I double-click on that movie clip, you can see the animation. Inside of that movie clip, on frame 1, I have an instance called boarderSolo.
If I double-click on that instance, I'll enter a movie clip called boarderSolo. You'll see that I have the basic bitmap shape with the boarder and then the skateboard inside of a movie clip, with an instance name of board. So let's say I want to communicate to the skateboard. In order to do that, I'll need to communicate to the boarder itself, which is inside of the boarder_mc movie clip. Let's go to the first keyframe of the actions layer, and open up the Actions panel. In here, under the line of code where I created the variable, I'll type boarder.boarderSolo.alpha = .25; so to access the solo boarder, I can simply use dot syntax.
So boarder, which is the outside boarder or boarder_mc.boardersSolo, is the boarder itself - not the animation. Then I can tweak that by modifying its properties with dot syntax. So let's test the movie. Now we see the boarder's alpha is .25, but watch when I click the boarder. The alpha goes up. Take a second to think about why that is. Are we modifying the boarder's alpha, or are we modifying the solo boarder's alpha? Look at the code. boarder, which is boarder_mc, or the animation, .boarderSolo.alpha, so we are only modifying that first frame.
So we are modifying the instance on that first frame of boarder_mc. Now take a second to think about how you would access the skateboard, if you only wanted to modify the skateboard. Just like accessing the solo boarder, you can access the skateboard through dot syntax. So after boarderSolo, type .board, and you can modify its properties. So let's see what happens when we set the x position of the board to 0.
I'll test the movie, and it appears that the code is not working, because the board doesn't seem to be moved; however, the board's position is at 0, relative to its parent movie clip. So if I want to move it backwards, I'd have to give it a negative value. So I could then type -25, test the movie, and the board has shifted 25 pixels to the left.
So using dot syntax, you can modify child and grandchild elements, but what if you want to modify a parent element? Let's look at how to do that. At the bottom of my code, I created a simple function called restart. This would represent restarting the game. All it does is it puts the boarder on the first frame and then sets his x position to 0. Let's say we wanted to run this function from inside of the boarder. Let's take a look at why you might want to do that.
Close the Actions panel and double-click the boarder on Stage to enter its Timeline. So let's say you click on the boarder and he plays his closing animation where he falls down. Once the animation is complete, you want to send the boarder back to his original state by running that restart function on the main Timeline. So here's an instance where you want some code to trigger after an animation has played, so it makes sense to put code inside of the Timeline of a movie clip.
I'll select the last keyframe with the actions layer, and open up the Actions panel. Make sure you're inside of that boarder movie clip. So that first frame says stop. Now to communicate to an object's parent, all you need to do is use the Parent command. So type parent.restart();. Now by itself, this code may not work. So if I test the movie, I get an error. Flash tells me, Call to a possibly undefined method restart.
Let me explain why this is happening. Flash knows who the parent of this object is; it's the main Timeline. Flash also knows that there is a restart function on the main Timeline. Here's the problem: the datatype for this parent property is not a movie clip; it is the display object, and a display object does not have a function called restart built-into it. So what I need do is tell Flash to treat this as a different type of object.
So before parent, I'll type Object, with a capital O, and wrap parent in parentheses. Object is basically a generic object that can have any properties and methods. So this is the easiest way to do this. Type Object, capital O, wrap parent in parentheses and after the close parenthesis, type .restart. Test the movie. You don't get any errors. If you click the boarder, he'll fall off, then return to the left edge of the Stage on frame 1. So this technique where we wrapped parent inside of Object and told Flash to treat it as a different type of data is called Typecasting.
So using the parent property with the Typecasting technique, you can access any object from within any timeline of your application.
Find answers to the most frequently asked questions about ActionScript 3.0 in Flash Professional CS5 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
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.