Friday, August 13, 2010

Closing a Flash Android App

I've just finished my first complete build of a Flash Game App for Android and I thought I'd share some code.

It's the little things that'll stump you, like figuring how to quit the app. So many apps make you use the HOME key and then the app continues running in the background. That's really frustrating. Use this line of code to kill your app:

NativeApplication.nativeApplication.exit();

And you'll need this import statement in your document class:

import flash.desktop.NativeApplication;

Another good thing to know is how to access the MENU, HOME and SEARCH keys. To do this, add a keyListener for a keyboard event:

stage.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);

...then:

function keyUpListener(e:KeyboardEvent):void {
if( e.keyCode == Keyboard.MENU ){ }
if( e.keyCode == Keyboard.HOME ){ }
if( e.keyCode == Keyboard.SEARCH ){ }
}

Don't forget to add this to your document class: import flash.ui.Keyboard;

Simples! A few lines to code to help you with your Flash App for Android.

Remember to support Android - Android loves Flash, unlike the iPhone.