Events
Quick example
Create a new as3 document.
On frame 1 open the actions panel and type the following code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, onkeyPressed);
function onkeyPressed(event:KeyboardEvent):void{
trace ("keycode is: " + event.keyCode);
}
Here a eventlistener is being added to the stage which will listen for a key being pressed. When a key is pressed the function onkeyPressed is called. This function simply traces the keycode (a unique number assigned to each key) for the key that triggered the event. The event.keyCode is simply receive the event that called the function so if you press k 75 will be displayed.
Control / test movie.
Part 1 Buttons
Create a new flash actionscript 3 document
Create a small button on frame 1 with the label Page 2. If you cannot remember how to do this then refer back to the creating buttons tutorial (in the flash CS4 tutorial), give the button an instance name of pageBT.
Now insert a blank keyframe on frame 5.
Select the text tool and on frame 5 type 'Page 2' using a large font 24 and colour black.
Insert a new layer and rename this actions. Insert a blank keyframe in frame 5 of this layer.
Your timeline should look like below without the small a in the keyframe on the actions layer (you have yet to add the code).
Select frame 1 of the actions layer and open the actions panel and type:
function page2(event:MouseEvent):void
{
gotoAndPlay(5);
}
pageBT.addEventListener(MouseEvent.CLICK, page2);
stop();
The stop(); simply prevents the playhead moving to the next frame. The line above this is the event Listener which is attached to the button with the instance name pageBT. Within the parenthesis is attached the event which is a mouse event and is listening for the mouse being clicked. When the mouse is clicked then page2 which is a function is then called.
The function is named page two and is executed when the mouse is clicked over the pageBT button. The void simply states that no parameters are being passed. Within the braces is the gotoAndPlay(5) which simply tells the playhead to go to frame 5 on the timeline.
On frame 5 open the actions panel and type:
stop();
Control / test movie. Save your file as buttonNavigation.fla
Excercise
Now see if you can add another button which will cause the play head to go back to frame 1.
Part 2
Create a new flash actionscript 3 document
Create a small button on frame 1 with the label Add One, give it the instance name of addBT.
In the top right hand corner of the stage create a dynamic text box with an instance name theNumber. Ensure that black is selected for the font.
The interface is now complete. Create a new layer and rename it actions and on frame 1 of this layer open the actions panel and type:
var count:uint = 0;
theNumber.text = count.toString();
function addOne(event:MouseEvent):void
{
count++;
theNumber.text = count.toString();
}
// you need to create the event listener for the button
stop();
The code is very similar to the previous example, but this time we are adding the value of 1 to the variable count everytime the button is pressed. The first line creates a variable named count which has a datatype of uint and a value of 0. The next line refers to the dynamic text box with the instance name theNumber, the .text indicates its the text attribute and the count.toString() converts the number to a string.
The event is the same as the previous example so I will not explain it again but this time within the function the varaible count is increment by 1 with the ++ and the new value is displayed.
Control / Test Movie
Save your file as addone.fla
