AS3 button click events vs As2 button click events example

this is a simple button tutorials for As3 beginners.

A simple As2 buttonClick

on(press){

trace("Clicked");

}


Or

[button].onPress=function(){

trace("clicked");

}

In as3

simple one

[button].addEventListener(MouseEvent.CLICK,function(){trace("clicked")});

or

function buttonClick(evt:MouseEvent) {

trace("clicked");

}

[button].addEventListener(MouseEvent.CLICK,buttonClick);


so what is in "evt:MouseEvent"?. just try this one

function buttonClick(evt:MouseEvent) {

trace(evt.target.name);

}

[button].addEventListener(MouseEvent.CLICK,buttonClick);

This event has the following properties:

Property Value
bubbles true
buttonDown true if the primary mouse button is pressed; false otherwise.
cancelable false; there is no default behavior to cancel.
ctrlKey true if the Control key is active; false if it is inactive.
currentTarget The object that is actively processing the Event object with an event listener.
localX The horizontal coordinate at which the event occurred relative to the containing sprite.
localY The vertical coordinate at which the event occurred relative to the containing sprite.
shiftKey true if the Shift key is active; false if it is inactive.
stageX The horizontal coordinate at which the event occurred in global stage coordinates.
stageY The vertical coordinate at which the event occurred in global stage coordinates.
target The InteractiveObject instance under the pointing device. The target is not always the object in the display list that registered the event listener. Use the currentTarget property to access the object in the display list that is currently processing the event.

2 comments:

tafaridh said...

just so folks know a click event is different than a press event.

a click event in AS3 is implies that the user clicked and released in the designated hit area.

the AS2 onPress is actually more like AS3's MouseDown event. these both with fire your event as soon as the mouse button goes down in the designated hit area. it may seem like a small difference but when building UI it's a good detail to pay attention to :)

Unknown said...

Thanks! I'm familiar with AS3 but forgot the format for AS2...


PHP developer Cochin |PHP developer Kerala | PHP developer India | Flash developer/programmer Cochin | Flash developer/programmer Kerala | Flash developer/programmer India | web programmer kerala |web programmer Cochin |web programmer India |web developer kerala |Web developer Cochin | Kerala PHP | PHP freelancer| flash actionscript freelancer