Action Script 3 On Click
Before i start i want to let you know today is my first day with AS3. I wanted to know how to do an onclick function in AS3. For example i have button 1 ( as Instance name) and when clicked i want it to hide and show another box. this is what i found online but how can i开发者_StackOverflow make it on click.
this.button1.alpha = 100;
Thanks so much.
You want
button1.addEventListener(EventType, callback);
You replace EventType
with a mouse event (such as MouseEvent.MOUSE_DOWN
) and callback
is a function that you define, which is called whenever the event occurs.
See the following example, taken from this page on the FlashEnabled Blog:
// attach the event listener to this object, if you want a global event outside
// the current class attach to stage.addEventListener([event],[callback])
this.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
// then make the callback
public function onMouseClickEvent(event:Event) {
trace(event);
if(event.buttonDown)
// if primary button down, left mouse button
trace(”left button was down”);
else
trace(”left button was not down”);
}
}
The above code sample attaches a click event handler to this
(whatever context this code is executed in - it could be global, or inside a class). Inside your event handler, you'd want to use the Tween
class (as explained on Kirupa.com) to animate the box out and the other box in.
Since you mentioned that it's your first day, note that trace()
writes to the console.
精彩评论