Skip to content

Instantly share code, notes, and snippets.

@sergebat
Created August 19, 2014 10:39
Show Gist options
  • Save sergebat/30b3b3546af3fadb72e7 to your computer and use it in GitHub Desktop.
Save sergebat/30b3b3546af3fadb72e7 to your computer and use it in GitHub Desktop.
Example on handling "Back" button in Ludei/easeljs/typescript game
declare var CocoonJS;
module hardware {
var backButtonHandler: () => void = null;
var backButtonDisplayObject: createjs.DisplayObject = null;
/**
* Call hardware.captureBackButton just before showing the screen, where you want to support "back" functionality,
* pass your own handler (typically screen onQuit handler).
*
* It assumes that only one screen is shown at a time.
*
* Passed currentScreen display object is only used to check that screen object is indeed visible and
* added to the stage. This call will be ignored (and default back button behavior will be used), if screen is hidden
* or removed from stage. This may happen if user quit this screen using in-game UI (not back button). This is bit
* hacky, but simplifies back button implementation in our simple usecase.
*/
export function captureBackButton(currentScreen: createjs.DisplayObject, handler: () => void) {
backButtonDisplayObject = currentScreen;
backButtonHandler = handler;
}
CocoonJS && CocoonJS.App.setAppShouldFinishCallback( () => {
if (!backButtonHandler || !backButtonDisplayObject || !backButtonDisplayObject.getStage() || !backButtonDisplayObject.visible) {
return true;
}
backButtonHandler();
return false;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment