Saturday, August 11, 2007

ActionScript 3.0 / Flash 9

Great surprise for all of us who in the years got used with ActionScript 2.0. First of all - we cannot apply actions directly to the movie clip (so forget about the really simple on(release){...} ).

If you click on a movie clip and you have the "Actions" window opened you will notice "Current selection cannot have actions applied to it"! At first it is irritating, but it seems to be a good point.

So how to make a "RollOver", "RollOut" etc. effects in AS3 ?

The first what we do - we have to import the Events package:

import flash.events.Event;

Now lets assume we have the movie clip on the stage and we named it "mymc". What we have to to is to add event handlers to it to listen for events:

mymc.addEventListener(MouseEvent.ROLL_OVER, RollOverHandler);
mymc.addEventListener(MouseEvent.CLICK, ClickHandler);
mymc.addEventListener(MouseEvent.MOUSE_OUT, RollOutHandler);

There is a set of predefined constants in the MouseEvent object, which we can use.

The next step is to implement the event handlers. Here is the simplest implementation of an event handler:

function RollOverHandler(me:MouseEvent):void {
    me.target.alpha = 1.0
}

I would rather call the type of "me" MouseEventArgs, but it's another story. What do we have in the MouseEvent variable is the "target". The "target" is the object that has escalated the event. Now we can say to that target what to do. We can say for example: me.target.gotoAndPlay(2), or me.target.gotoAndStop(1);

OK, that's it so far. Hope to have more to write soon ;)

No comments: