I’m reading a lot of examples and I know this is very easy, but I’m stuck =/
I’m using a class caled (KeyboardInputs) to dispatch events (keyboard/custom events…).
The idea is that this Class would dispatch all Key Down events so EVERY class could listen to it.
Thing is, events are being dispatched ok, but I’m not sure the Classes are listening to it correctly. I mean. When I create a new instance of KeyboardInputs inside each class, and add a listener to it, are all the listener listening to the same events?
this is a sample of what I’ve got:
MainClass.as:
...extends Sprite...
KeyboardInput.as
package lib{
import flash.events.*
import lib.KeyMap;
public class KeyboardInput extends MainClass
{
public function keyboardInput()
{
addEventListener(Event.ADDED_TO_STAGE,function(ev:Event):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,function(e:KeyboardEvent):void
{
keymap = new KeyMap();
keymap.setKey(String.fromCharCode(e.charCode))
});
});
}
}
}
KeyMap.as
package lib
{
import flash.events.EventDispatcher;
import lib.ConnectionEvent;
public class KeyMap extends EventDispatcher
{
public function KeyMap()
{}
public function setKey(_k:String)
{
dispatchEvent(new ConnectionEvent(ConnectionEvent.ON_KEYPRESS,_k));
}
}
}
ConnectionEvent.as
package lib
{
import flash.events.Event;
public class ConnectionEvent extends Event
{
public static const ON_KEYPRESS:String = "onKeyPress";
public var data:String;
public function ConnectionEvent(type:String, ret:String = null)
{
super(type);
if (ret != null) {
data = ret;
}
}
public override function clone():Event
{
return new ConnectionEvent(type);
}
}
}
HomeScreen.as
package scr{
import flash.display.*
import lib.*
public class HomeScreen extends Sprite {
public var _keymap:KeyMap;
public function HomeScreen()
{
_keymap.addEventListner(ConnectionEvent.ON_KEYPRESS,keyAction);
function keyAction(ev:ConnectionEvent)
{
**[COLOR="red"] trace("trace something")// <-- not outputing
//DO SOMETHING HERE, BUT THIS IS NOT LISTENING TO NOTHING! =/[/COLOR]**
}
}
}
}