I am making a game and I am having problems getting a class to handle a custom event… I know the events are firing because I put some trace statements into my custom event class.
Any help here is much appreciated…
Here is how it’s set up:
I wrote a Singleton class to handle all game controls:
package {
//Imports
import flash.display.Stage; //So a reference to the stage can be held without adding to display list
import flash.events.KeyboardEvent; //To listen to keyboard events
import flash.events.EventDispatcher; //To dispatch events
//Controls Class handles all Keyboard interaction
public class GameControls extends EventDispatcher{
//Holds a static version of this class
private static var _instance:GameControls;
//Instance variable holds reference to the stage
private static var _stage:Stage;
//Constructor
public function GameControls(enforcer:SingletonEnforcer) {
//Check for an instantiation attempt using a null argument
if(enforcer == null) {
throw new Error("Cannot be instantiated - Singleton Class");
} else {
//Add keyboard listeners to the stage
_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
_stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
}
public static function getInstance(stageRef:Stage = null):GameControls {
//Assign stage reference
if (stageRef != null) _stage = stageRef;
if(GameControls._instance == null) {
GameControls._instance = new GameControls(new SingletonEnforcer());
}
return GameControls._instance;
}
//Handles key presses/holds
private function keyDownHandler(e:KeyboardEvent):void {
//Check for 'Enter', 'Shift', or 'P'
if(e.keyCode == 13 || e.keyCode == 16 || e.keyCode == 80) {
dispatchEvent(new MenuEvent(MenuEvent.KEY_DOWN, true, false, e.keyCode));
} else {
dispatchEvent(new ShipEvent(ShipEvent.KEY_DOWN, true, false, e.keyCode));
}
}
//Handles when key lifts occur
private function keyUpHandler(e:KeyboardEvent):void {
//Check for 'Enter', 'Shift', or 'P'
if(e.keyCode == 13 || e.keyCode == 16 || e.keyCode == 80) {
dispatchEvent(new MenuEvent(MenuEvent.KEY_UP, true, false, e.keyCode));
} else {
dispatchEvent(new ShipEvent(ShipEvent.KEY_UP, true, false, e.keyCode));
}
}
//Garbage collection
public function kill():void {
_stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
_stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
_stage = null;
}
}
}
internal class SingletonEnforcer{}
I have a custom ShipEvent class:
package {
//Import in order to inherit
import flash.events.Event;
//Custom event class fires all ship events
public class ShipEvent extends Event {
//Event types
public static const KEY_DOWN:String = "key down";
public static const KEY_UP:String = "key up";
//Holds button pressed
private var _userInput:uint;
//Constructor
public function ShipEvent(type:String, bubbles:Boolean, cancelable:Boolean, userInput:uint) {
super(type, bubbles, cancelable);
_userInput = userInput;
}
//Gets user input
public function get userInput():uint {
return _userInput;
}
//Clone method
override public function clone():Event {
return new ShipEvent(type, bubbles, cancelable, _userInput);
}
}
}
And finally, I have a Ship class that needs to be able to react to the ship controls:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Ship extends MovieClip {
public function Ship() {
this.addEventListener(ShipEvent.KEY_DOWN, keyDownHandler);
}
private function keyDownHandler(e:ShipEvent):void {
trace(1, e.userInput);
}
}
}
Here is the document class that I am using to test:
package {
import flash.display.Sprite;
public class TestRunner extends Sprite {
public function TestRunner() {
GameControls.getInstance(stage);
var ship:Ship = new Ship();
this.addChild(ship);
}
}
}
Thoughts?
AR