I'm making a game for Android using AIR (meaning it's programmed in ActionScript 3, same as Flash).
What I'd like to do is to make the physical back button on the phone NOT exit the game, it should pause the game instead. (I will make it so that it will still exit the game if pressed twice rapidly.)
However my code is not working:
public function Main() {
if (Capabilities.cpuArchitecture=="ARM") {
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onMainKeyDown);
开发者_如何学JAVA }
}
private function onMainKeyDown(ke:KeyboardEvent) {
if (ke.keyCode==Keyboard.BACK) {
// Pause the game here.
ke.preventDefault();
ke.stopImmediatePropagation();
}
}
When I publish the thing to my device it still exits when I'm pressing the physical back button on the phone.
What am I doing wrong here?
Edit: There was just a null pointer exception issue I hadn't discovered yet. How embarrassing!
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true)
function onKeyDown(event:KeyboardEvent):void
{
if( event.keyCode == Keyboard.BACK )
{
event.preventDefault();
event.stopImmediatePropagation();
//handle the button press here.
}
}
Note that if you’ve set stage.displayState = FULL_SCREEN, no keyboard events are sent to your app! Use stage.displayState = FULL_SCREEN_INTERACTIVE instead!
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Here you can do what ever you want to do while pressing the back button
}
return true;
}
精彩评论