Button Events
Rotating a Movie Clip
Download alien.fla
Open the actions panel and type the following code on frame 1 of the actions layer
var spinning:uint = 0;
spinBT.addEventListener(MouseEvent.CLICK, spin);
function spin(event:Event):void{
spinning+=10;
alien_mc.rotation = spinning;
}
The first line create a positive integer with a value of 0 assigned to the variable spinning. The next line is the event listener which is a mouse event attached to the button with an instance name spinBT. When the button is clicked the spin function is then called. The spin function simply increments the value of spinning by 10 and then rotates the movie clip alien_mc by this amount.
Control / Test Movie to try this out.
Exercise
See if you can add another alien, another button to get the other alien to spin.
Feeding the alien
Downlaod alienfeed.fla
Select the banana button and give it an instance name of bananaBT.
Select the apple button and give it an instance name of appleBT.
Then select frame 1 of the actions layer and open the actions panel and type:
import flash.events.*;
var belly:uint=0;
var apple:uint=25;
bellyDT.text = String(belly); // convert to string
appleBT.addEventListener(MouseEvent.CLICK, feed);
function feed(event:Event):void{
belly+=apple;
bellyDT.text = String(belly);
}
The import statement on the first line is mainly used when creating Classes and is used to import the flash events into the class, it is not really necessary in this application but I thought I would introduce it here. The next two lines creates two variables one for the belly and one for the value of the apple. The next is the dynamic text box and the value of the variable belly is displayed in the dynamic text box. Following this we have the listener attached to the appleBT again using a mouse event click. The function feed simply increments the belly variable by the amount stored in the apple variable and then updates the dynamic text box with the new amount.
Control / Test movie to test the application.
Exercise
Add the functionality to the banana button this should increase the amount by 45.
