Jump to content

Phaser game+CocoonJS, handle Android hardware back button?


BdR
 Share

Recommended Posts

I'm testing a game on CocoonJS for Android and I'm trying to handle the Android hardware "back button". Basically it should behave like a normal Android app, so that where pressing the hardware back button it goes to a previous screen, for example from the level select back to the main menu.

 

I've downloaded and included the cocoon.min.js file from GitHub (see here) so I can access a cocoon object class, and I've added code like so:



    Cocoon.App.exitCallback(function(){
        switch (this.game.state.current) {
            case 'Tutorial':
                this.game.state.states['Tutorial'].doBtnBack();
                return false;
                break;
            case 'LevelSelect':
                this.game.state.states['LevelSelect'].doBtnGoback();
                return false;
                break;
            default:
                return true;
        };
    });


But when I run the app in the CocoonJS launcher and I press the back button it gives an error on the line "switch (this.game.state.current)" because game is undefined.

 

So I know I'm on the right track because the code gets called, but what am I doing wrong?

Link to comment
Share on other sites

You have a problem because anonymus function have a own scope, U must overwrite it to have access to scope with game object.

 

 

Try it:

Cocoon.App.exitCallback(function(){        switch (this.game.state.current) {            case 'Tutorial':                this.game.state.states['Tutorial'].doBtnBack();                return false;                break;            case 'LevelSelect':                this.game.state.states['LevelSelect'].doBtnGoback();                return false;                break;            default:                return true;        }.bind(this);    });

or

Cocoon.App.exitCallback((function(){        switch (this.game.state.current) {            case 'Tutorial':                this.game.state.states['Tutorial'].doBtnBack();                return false;                break;            case 'LevelSelect':                this.game.state.states['LevelSelect'].doBtnGoback();                return false;                break;            default:                return true;        })(this);    });
Link to comment
Share on other sites

You have a problem because anonymus function have a own scope, U must overwrite it to have access to scope with game object.

 

Thanks :) You are right, that's what caused my problem.

 

My code that set the exitCallback was in an anonymous function, so I moved that part into one of my game's States objects so it can reference the game object (because my "var game" is also created in its own anonymous function) and then I added your .bind part. There were some incorrect brackets in your example code, but I managed to get it working using this code below. 

MyGame.MainMenuState = function(game){    this.game = game;    // handle the Android back button    Cocoon.App.exitCallback(            function() {                //console.log('** back button was pressed!! **');                //..can use this.game            }.bind(this)    );};MyGame.MainMenuState.prototype = {    create: function(){    //etc.
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...