Monday, November 10, 2008

Zoiks! on() is GONE in CS3

Wow AS3 dumps alot of the code tools were used to using!


The truth of the matter is, ActionScript 3.0 is different, in many respects, from AS2 or 1. (As it happens, it’s also the same in many respects, which can be a source of frustration for experienced users, but chin up!) One of the most basic, familiar things people do in Flash is to assign some ActionScript to a button. In former versions of ActionScript, this could be done in a number of ways, as explained a bit in “Museum Pieces: on() and onClipEvent().” In AS3, the rules have changed. There were at least four ways to handle events in the past — on() and onClipEvent(), dot notation, addListener(), and addEventListener() — and AS3 mercifully reduces those four down to one consistent approach. Honestly, this makes it easier.
Here’s how it goes. Let’s say you have a button symbol on the Stage. Select it, give it an instance name in the Property inspector. Now that it has an instance name, it has become accessible to ActionScript, which now has a unique way of identifying it from any other objects present. In a keyframe script (select a keyframe in line with the button, preferably a keyframe on its own layer), type the following:

myButton.addEventListener(
MouseEvent.MOUSE_UP,
function(evt:MouseEvent):void {
trace("I've been clicked!");
}
);



And that’s it. Why not select the button itself? What’s wrong with good old on()? For better or worse, direct attachment of code is a thing of the past. ActionScript 3.0 simply doesn’t support it. In fact, you’ll notice that the Behaviors panel is dimmed when a FLA’s Publish Settings are configured for AS3. Why? Because Behaviors rely on on() or onClipEvent(). Change your Publish Settings to AS2 or 1, they come back.

No comments: