Flash Essential

AS3 Guide Functions

Sep 2nd 2008
2 Comments
respond
trackback

What are ActionScript 3.0 Functions?

A function is a reusable block of code. ActionScript code executes in linear fashion but with the use of functions allows us to execute code when and how we want. For example you may want a certain part of code to execute when a mouse click occurs. Below we'll go through how to write a function that will change the alpha setting of an object on the stage when hover your mouse over that object that object. Lets go through the code below.

Show Navigation || Hide Navigation

On the stage we have drawn a rectangle, converted it to a movie clip and given that movie clip the instance name myObject.

First we give our myObject movie clip an event listener which is listening for a mouse over event. We'll cover event listeners later in the series.

myObject.addEventListener(MouseEvent.MOUSE_OVER, MouseOver);

function MouseOver(evt:MouseEvent):void{
myObject.alpha = .5
}

When writing functions we start out with the function keyword, we then give it a unique name in this case it's MouseOver which is the same inside the brackets of our Event Listener. Inside the brackets we have our arguments which essentially are variables that can only be used by their own functions, in the case below the argument is an Mouse Event. Finally we close off the brackets and write void as we aren't returning any values.

myObject.addEventListener(MouseEvent.MOUSE_OVER, MouseOver);

function MouseOver(evt:MouseEvent):void{
myObject.alpha = .5
}

Inside the our function we have the code we want to execute, We set myObject alpha setting to .5 which is 50%.

myObject.addEventListener(MouseEvent.MOUSE_OVER, MouseOver);

function MouseOver(evt:MouseEvent):void{
myObject.alpha = .5
}


This post is tagged ,



Sponsors

Explore Recent





Monthly Archives



Friends and Affiliates



2 Comments

Leave a Reply