|We have completed our FlashR CS4 eCard animation, added the stop() code and built the button that our viewer will use to replay the animation a second or subsequent time. Now we are ready to attach some ActionScript 3 code to our button. This code will tell Flash to listen for a mouse event, such as our button being clicked. Then we will write a function to tell Flash what to do when the button is clicked. Our function will tell Flash to go back to the first frame and play the animation again.|
Open your working file into Flash CS4.
1. First we need to add our Replay button onto the stage. We do not want the button to be available until the animation plays through the first time and stops. So, let's add the button to the last frame of the animation (Frame 75). Also to prevent the button from showing on the stage until Frame 75, we need to add a Keyframe. Right-click on Frame 75 of the Button layer and add a Keyframe. Flash will give you 74 blank frames and a keyframe on Frame 75.
Now, drag an instance (copy) of the Replay button from the Library onto the snow at the bottom right corner of the stage.
2. In order to control this instance (copy) of the Replay button with ActionScript, we need to give it a unique name. With the button selected, go to the Properties panel and replace "Instance Name" with "replay_btn". Now we can start working on the ActionScript.
3. Click on Frame 75 of the Actions layer and open the Actions panel. Here is our code so far.
4. Let's tell Flash to listen for a mouse click on our replay_btn button. We will use Script Assist again to write our ActionScript. Click on the Script Assist button to open the top pane.
Click on the Plus sign to open the list of ActionScript codes. From this code list, click flash.events – IeventDispatcher – Methods – addEventListener. Flash will automatically update our code by adding these two lines.
Join us in the Flash forum.