add event listeners to global stage via class AS3
i am making a simple game i want to be able to add an keyboard event lisnter within the class constructor. However i am having trouble. i undertsand you have to pass the stage through a listed display object property stage? anyway i am getting errors that do not recognise my event listener keyboard event.
Best Louis
package louiseguchi.game
{
import flash.display.Stage;
import flash.events.Event;
import flash.display.Sprite;
import flash.ui.Keyboard
import flash.events.KeyboardEvent
{
public function keyBoardVelocity extends EventDispatcher ()
{
// constructor开发者_C百科 code
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressUp);
}
private static function keyPressUp(Event:KeyboardEvent):void{
trace("ok")
}
}
}
You can create a class with a public static property that you would assign the stage reference to thereby making the stage accessible throughout the application like in the following:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
GlobalVars.stage = stage;
var keyboardVelocity:KeyboardVelocity = new KeyboardVelocity();
}// end function
}// end class
}// end package
import flash.display.Stage;
internal class GlobalVars
{
public static var stage:Stage;
}// end class
import flash.events.EventDispatcher;
import flash.events.KeyboardEvent;
internal class KeyboardVelocity extends EventDispatcher
{
public function KeyboardVelocity()
{
var stage:Stage = GlobalVars.stage;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
}// end function
private function onStageKeyDown(e:KeyboardEvent):void
{
trace("key down");
}// end function
}// end class
Note: I've used internal classes so that you can simply copy and paste the code into you document class and run it. Ideally you would separate the classes into their own files.
Personally I'm not a fan of making objects globally accessible as it poses a potential security risk and is bad practice in my opinion. You should consider simply parsing a reference of the stage to your KeyboardVelocity
class like in the following:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var keyboardVelocity:KeyboardVelocity = new KeyboardVelocity(stage);
}// end function
}// end class
}// end package
import flash.display.Stage;
import flash.events.EventDispatcher;
import flash.events.KeyboardEvent;
internal class KeyboardVelocity extends EventDispatcher
{
public function KeyboardVelocity(stage:Stage)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
}// end function
private function onStageKeyDown(e:KeyboardEvent):void
{
trace("key down");
}// end function
}// end class
A few problems here. For one, your class is malformed. See my comments.
package louiseguchi.game
{
import Main; //doc class
import flash.display.Stage;
import flash.events.Event;
import flash.display.Sprite;
import flash.events.EventDispatcher;
import flash.ui.Keyboard
import flash.events.KeyboardEvent
//only classes may be extended
public class KeyBoardVelocity extends EventDispatcher
{
//here is your constructor
public function KeyBoardVelocity()
{
super()
init()
}
private function init():void
{
addEventHandlers();
}
private function addEventHandlers()
{
//static access of main stage
Main.stage.addEventListener(KeyboardEvent.KEY_UP, keyPressUp);
}
// instance function
private function keyPressUp(Event:KeyboardEvent):void{
trace("ok")
}
}
}
Note that I added a few steps in between the constructor and your listener. Do so if you like, but I generally put very little in a constructor.
Secondly, you need to have some object in the display list to access the stage, and generally its accessed via the Main document class. Here, I just have a static get function that references in the document class:
package com.b99.testBed //alter as needed
{
import com.b99.testBed.keypress.KeyBoardVelocity;
import flash.display.Sprite;
import flash.display.Stage;
/**
* ...
* @author bosworth99
*/
public class Main extends Sprite
{
private static var _stage:Stage;
public function Main()
{
super();
init();
}
private function init():void
{
_stage = this.stage;
var _candidate:KeyBoardVelocity = new KeyBoardVelocity();
}
public static function get stage():Stage { return _stage; }
}
}
You could gain access to the stage in a number of ways, this is the one I generally prefer.
Hope that helps.
精彩评论