Basically I’m trying to just barely add keyboard event listeners in my program, then start building on them iteratively. The problem is that even getting the simplest stuff in there, derived from multiple tutorials, is apparently something an issue. I’m not too sure what I’m missing right now. I’ve tried adding stuff, changing stuff, and taking stuff out. But given the following code, I don’t see any messages showing up on the screen. Why?
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:local="*" xmlns:mx="http://www.adobe.com/2006/mxml" width="480" height="600" layout="absolute" creationComplete="onCreationComplete()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.events.KeyboardEvent;
private function onStart(pEvent:Event):void {
vs.selectedChild = mm;
}
private function onNewGame(pEvent:Event):void {
vs.selectedChild = game;
}
private function onCreationComplete():void {
addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(KeyboardEvent.KEY_UP, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyDown);
}
private function onKeyDown(pEvent:KeyboardEvent):void {
Alert.show("spam");
Alert.show(pEvent.toString());
}
]]>
</mx:Script>
<mx:ViewStack id="vs" creationComplete="vs.selectedChild = ts">
<local:TitleScreen id="ts" creationComplete="ts.addEventListener(TitleScreen.START, onStart);" />
<local:MainMenu id="mm" creationComplete="mm.addEventListener(MainMenu.NEW_GAME, onNewGame);" />
<local:Game id="game" />
</mx:ViewStack>
</mx:Application>
You can not use
stagedirectly. Instead this use this code: