External Exam Download Resources Web Applications Games Recycle Bin

event listeners

quick reference for event listeners:

stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
stage.addEventListener(Event.ENTER_FRAME, step);
stage.addEventListener(MouseEvent.CLICK, clicked);

//key pressed:
function key_down(e:KeyboardEvent):void {
	trace(e.keyCode);
}

//key released:
function key_up(e:KeyboardEvent):void {
	trace(e.keyCode);
}

//step event (each frame):
function step(e:Event):void {
	//trace(currentScene.name);
    //trace(currentFrame);
    if (currentScene.numFrames == currentFrame){ 
        gotoAndPlay(1,"MainGame"); 
    } 
}

//mouse clicked:
function clicked(e:MouseEvent):void {
	var x = e.stageX; //x click location
	var y = e.stageY; //y click location
	
	//add any shape to the stage, name it some_instance:
	if (some_instance.hitTestPoint(x,y,true)){
		trace("hit some_instance!")
        //before rerouting frames:
        //stage.removeEventListener(Event.ENTER_FRAME, step); 
        //stage.removeEventListener(MouseEvent.CLICK, clicked); 
	}
	
	//hitTestPoint(x,y,Boolean), where Boolean is:
	//  - true: precise, testing actual pixels
	//  - false: quicker, bounding box of object
}