Programming ActionScript 3.0 |
|
|
|
| Getting started with ActionScript > Example: Animation portfolio piece | |||
This example is designed to give you a first opportunity to see how you can piece together bits of ActionScript into a complete, if not ActionScript-heavy, application. The animation portfolio piece is an example of how you could take an existing linear animation (for example, a piece created for a client) and add some minor interactive elements appropriate for incorporating that animation into an online portfolio. The interactive behavior that we'll add to the animation will include two buttons the viewer can click: one to start the animation, and one to navigate to a separate URL (such as the portfolio menu or the author's home page).
The process of creating this piece can be divided into these main sections:
Preparing to add interactivity
Before we can add interactive elements to our animation, it's helpful to set up the FLA file by creating some places to add our new content. This includes creating actual space on the Stage where buttons can be placed, and also creating "space" in the FLA file for keeping different items separate.
To set up your FLA for adding interactive elements:Next we'll need to actually create and position the buttons that will form the center of our interactive application.
To create and add buttons to the FLA:playButton.homeButton.The ActionScript code for this application can be divided into three sets of functionality, although it will all be entered in the same place. The three things the code needs to do are:
To create code to stop the playhead when it enters Frame 1:stop();
To write code to start the animation when the play button is clicked:
function startMovie(event:MouseEvent):void
{
this.play();
}
This code defines a function called startMovie(). When startMovie() is called, it causes the main timeline to start playing.
playButton.addEventListener(MouseEvent.CLICK, startMovie);
This line of code registers the startMovie() function as a listener for playButton's click event. In other words, it makes it so that whenever the button named playButton is clicked, the startMovie() function is called.
To write code to send the browser to a URL when the home page button is clicked:
function gotoAuthorPage(event:MouseEvent):void
{
var targetURL:URLRequest = new URLRequest("http://example.com/");
navigateToURL(targetURL);
}
This code defines a function called gotoAuthorPage(). This function first creates a URLRequest instance representing the URL http://example.com/, and then passes that URL to the navigateToURL() function, causing the user's browser to open that URL.
homeButton.addEventListener(MouseEvent.CLICK, gotoAuthorPage);
This line of code registers the gotoAuthorPage() function as a listener for homeButton's click event. In other words, it makes it so that whenever the button named homeButton is clicked, the gotoAuthorPage() function is called.
At this point, the application should be completely functional. Let's test it to make sure that's the case.
To test the application:addEventListener() method calls use the same names as the buttons' instance names?addEventListener() method calls?All of these and most other possible mistakes should give an error message either when you choose the Test Movie command or when you click the button. Look in the Compiler Errors panel for compiler errors (the ones that happen when you first choose Test Movie), and check the Output panel for run-time errors (errors which happen while the SWF is playing--such as when you click a button).
|
|
|
|