AddThis Social Bookmark Button
 Mouse events in Actionscript 3.0 (part1)

So you just started AS3.0 full of excitement and ready for some coding challenges, you start coding your first button and suddenly, horror, you end up with "WARNING: Actions on button or MovieClip instances are not supported in ActionScript 3.0. All scripts on object instances will be ignored." or "1119: Access of possibly undefined property onPress through a reference with static type flash.display:SimpleButton.", indeed the good old AS2.0 event system is not working anymore in AS3.0: onClipEvent, onPress, onRelease, onLoad, onMouseDown ..etc, have become things of the past!!!

What happened? well basically the whole event handling system has been revamped in Actionscript 3.0 and we are going to have a quick look at it. Since most of the Flashvalley users are looking for simple explanations and practical tutorials I am not going to go into a complex discussion about the new event handling system but we will quickly go in practical working examples.

What you need to know is that the most noticeable difference between event handling in ActionScript 3.0 and event handling in previous versions of ActionScript is that in ActionScript 3.0 there is only one system for event handling, whereas in previous versions of ActionScript there are several different event-handling systems.

in AS2.0 events can be handled differently and following different patterns:

  • on() event handlers that can be placed directly on Button and MovieClip instances
  • onClipEvent() handlers that can be placed directly on MovieClip instances
  • Callback function properties, such as XML.onload and Camera.onActivity
  • Event listeners that you register using the addListener() method
  • The UIEventDispatcher class that partially implemented the DOM event model.

In AS3.0, and to summarize, all events are handled with the following logical pattern:

function eventListener(eventObject:EventType) {
// Actions performed in response to the event go here.
}

eventTarget.addEventListener(EventClass.EVENT_NAME, eventListener);


In AS3.0 there is a wide range of events types, representing everything from user input to network and sound activity. Before setting up your event you will first need to find the correct event class along with the correct constant associated to that particular type of event

Let's put that into practise with a couple of examples.

1. Mouse events

In AS3.0 all interactivities based on mouse clicks, mouse wheel and mouse moves are now going to be handled with that new event system.

if you want to to detect a mouse click anywhere on the stage you will need to attach and to define a listener to the stage object like below:

function myClick(eventObject:MouseEvent) {
trace("mouse click detected");
}

stage.addEventListener(MouseEvent.CLICK, myClick);

The name of the event is defined by the constant associated with the event class. In our case CLICK is one of the events associated with the MouseEvent class, the events defined for the MouseEvent class are CLICK, DOUBLE_CLICK, MOUSE_DOWN, MOUSE_UP, MOUSE_MOVE, MOUSE_OUT, MOUSE_OVER, MOUSE_WHEEL, ROLL_OUT, ROLL_OVER.

To attach a behaviour to a button in AS 2.0 you would attach the following code to that button:

on(release){
// do something
}


or if the button is a movie clip you will attach:

onClipEvent(load){
this.onPress = function(){
// do something
}
}


or you in both cases you could define the behaviour in a frame of the timeline:

myButton.onPress = function(){
// do something
}
}

AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.
flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved