/*In this tutorial on events, I'm going to explain how to use events in AS3 to
manipulate objects and stuff when certain things (called events) happen.
First Up: Import Events
*/
import flash.events.MouseEvent;
// includes events such as click, mousedown, mouseup, roll over, etc.
import flash.events.Event;
// the basic frame events
import flash.events.KeyboardEvent;
// keyboard events, such as key down.
// import flash.events.* will import all types of events.
// you dont actually need to import events unless you are writing a class.
//
// Adding events to the stage:
stage.addEventListener(Event.ENTER_FRAME,ente rFrameFunction);
// this adds an enterframe event, meaning updated everyframe, to the stage,
// linked to a function called enterFrameFunction, which is below
// a function is a piece of code that performs a certain task when there are
// variables in the task that differ each time the function needs to be called.
// or, in the case of event functions, they are just used to set aside a certain
// amount of code to be executed at certain times.
// A listener is an object that listens in the .swf file for certain events, and
// calls its function when it hears the events.
function enterFrameFunction(event:Event):void {
// code that will happen every frame
// the :void means that the function returns nothing. It will always be void
// on an event function because there is no place to return something to because
// there is not way to call an event function. That is the job of the event
// listener.
}
stage.addEventListener(MouseEvent.CLICK,click Function);
// adds a click event, meaning press+release over the same object
function clickFunction(event:MouseEvent):void {
// code that will happen on click
}
// from now on, I'm not going to explain the code so much, since it should be fairly
// straightforward
stage.addEventListener(MouseEvent.MOUSE_DOWN,
mouseDownFunction);
function mouseDownFunction(event:MouseEvent):void {
}
stage.addEventListener(MouseEvent.MOUSE_UP,mo useUpFunction);
function mouseUpFunction(event:MouseEvent):void {
}
// A list of mouse events:
/* MOUSE_MOVE
MOUSE_OUT
MOUSE_OVER
MOUSE_WHEEL
DOUBLE_CLICK may give you some problems
ROLL_OVER
ROLL_OUT */
// And some frame events:
/* UNLOAD
FULLSCREEN
ADDED
REMOVED
ACTIVATE
INIT
MOUSE_LEAVE */
//Keyboard events are just KEY_DOWN and KEY_UP
stage.addEventListener(KeyboardEvent.KEY_DOWN ,keyDownFunction);
function keyDownFunction(event:KeyboardEvent):void {
trace(event.keyCode)
// the event specifies the type of event that was called, and the keyCode is
// a property of the KeyboardEvent which tells what key was pressed.
}
//
//Adding Events to objects
//
import flash.display.*;
var circle:Sprite = new Sprite()
with (circle.graphics) {
lineStyle(1)
beginFill(0xFF0000,1)
drawCircle(10,10,10)
}
addChild(circle)
// put the mc on the stage.
circle.addEventListener(Event.ENTER_FRAME,cir cleFrame);
// the circle in front of addEventListener specifies that the listener is being
// add to the circle rather than the stage.
function circleFrame(event:Event):void {
//code here
//use event.target to change the properties of the circle
event.target.x++;
}
circle.addEventListener(MouseEvent.MOUSE_DOWN ,circleDown);
function circleDown(event:MouseEvent):void {
trace("you pressed the circle");
}
//
//removing listeners
//
circle.removeEventListener(Event.ENTER_FRAME,
circleFrame)
// the parameters must be the same as those specified in the addEventListener function
thanks for reading, I hope you understand how to use events in AS3 now.