Quantcast
Channel: Video Training - Tutorials » Uncategorized
Viewing all articles
Browse latest Browse all 2

Flash CS3 Tutorial – Event Handling

$
0
0

For users who prefer to learn Flash visually we have a range of Adobe Flash CS3 video tutorials, this method of training greatly enhances learning and allows beginners to master even the most complex aspects of Adobe Flash with ease.
View the Flash CS3 Tutorial Videos .

.

Interactivity with Flash – Event Handling
The code used in this Flash Tutorial is mainly ActionScript 2.0 with notes for using ActionScript 3.0

This tutorial comprises an introduction to interactivity in Flash, including the underlying principle of Event Handling.
Please note that Event Handling changed significantly between ActionScript 2.0 and 3.0.

Interactivity is one of the main reasons for using Flash to deliver applications and components. It allows you to create rich and stimulating experiences for the user with relative ease. The core concept for delivering interactive results in Flash is Event Handling. A number of functions within the ActionScript language provide you with the means to detect and respond to user interaction. Each time the user ‘interacts’ with your Flash movie, for example using the mouse, one or more ‘events’ is generated; your ActionScript code then has the ability to ‘handle’ these events.
To demonstrate, we will create a simple Flash movie with some interactivity built in. Open Flash and create a new document, then draw a shape on the stage and convert it to a symbol (with the shape selected, press F8 of choose Modify > Convert to Symbol). Give the symbol a name and select Movie clip as the type:

Now give the clip an instance name by selecting it on the stage and entering ‘myclip_mc’ as the Instance Name on the Properties panel (Window > Properties > Properties or F3 if it isn’t visible):

Create a new layer in your movie (press the Insert Layer button on the timeline or choose Insert > Timeline > Layer):

With the new layer selected, open the Actions Panel (press F9, choose Window > Actions or click on it if it’s already visible); this is where we will keep our ActionScript code. First we will detect the user clicking on the clip symbol, in order to do this enter the following code (ActionScript 2.0):

myclip_mc.onPress = function() { trace(“press”); };

This code instructs the movie to listen for the mouse press event on the movie clip symbol, and, when a mouse press is detected, to execute the code within the brackets. This statement is assigning a function to the movie clip onPress event, meaning that Flash Player should carry out the function whenever the event fires.

If you’re using ActionScript 3.0, your code will be slightly different:

myclip_mc.addEventListener(“onPress”, clipPressed);
function clipPressed() { trace(“press”); };

This code has the same effect as the code above. It tells the movie to listen for the onPress event on myclip_mc and then execute the code within the clipPressed function specified.

Test your movie (CTRL + Enter or Control > Test Movie); click on the shape, you should see “press” written to the output panel:

To demonstrate a different event, change onPress to onRelease in your ActionScript code and test the movie again; you should see that the output only now appears when you release the mouse button (test this by clicking and holding it for a moment before release).

There are many events that ActionScript can listen for. For example, to detect the mouse cursor rolling over your movie clip, change onPress (or onRelease) to onRollOver. Test your movie, then roll the mouse on and off the shape; the event should fire each time the cursor moves onto the shape. Try changing onRollOver to onRollOut and test again. This time the event fires when the cursor moves off the shape.

Experiment with the different events to familiarise yourself with them; you can also use button symbols, which have a degree of interactivity built-in, meaning that you may need to write less (if any) ActionScript code yourself.


Tagged: ActionScript 3, Flash CS3, Flash Event Handling, Tutorials

Viewing all articles
Browse latest Browse all 2

Trending Articles