I’m new in AS, and trying to make my first application. I have a class, that showld manage some events, like keyboard key pressing:
public class GameObjectController extends Sprite
{
var textField:TextField;
public function GameObjectController()
{
addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
private function onKeyDown(event: KeyboardEvent):void
{
trace("123");
}
}
but when I’m running it, and pressing any button, nothing happands. What am I doing wrong?
Try
stage.addEventListener()forKeyboardEvents.The reason for this is that whatever you add the event to needs focus, and the
stagealways has focus.If
GameObjectControllerisn’t the document class, it will need to havestageparsed to its constructor for this to work, or it will need to be added to the stage first. The former will be less messy to implement.