JS - Not working in android - App got closed

var MenuLayer = cc.Layer.extend({

ctor:function () {
    //////////////////////////////
    // 1. super init first
    this._super();
    var size = cc.winSize;

    var sprite = new cc.Sprite(res.bg_2);
    sprite.setPosition(size.width / 2, size.height / 2);
    sprite.setScale(1);
    this.addChild(sprite, 0);        

    var menuItem1 = new cc.MenuItemImage("res/play.png", "res/play_hover.png", play);
  

    var menu = new cc.Menu(menuItem1);
    menu.setPosition(size.width / 2, size.height / 2);
    menu.alignItemsVerticallyWithPadding(10);
    this.addChild(menu, 1);

    return true;
}
});

var play = function() {
    var scene = new GameScene();
    cc.director.pushScene(new cc.TransitionZoomFlipY(0.3, scene));
}

var MenuScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new MenuLayer();
        this.addChild(layer);
    }
});

This coding got executed with any problem. When i press the button, app got closed. I removed all the coding in GameScene() But it didnt work.

var GameScene = cc.Scene.extend({
onEnter:function () {
    this._super();
    var layer =  new GameLayer();
    this.addChild(layer);
}
});

var GameLayer = cc.Layer.extend({
ctor: function() {
	this._super();
	var sprite = cc.Sprite("res/img.png");
	this.addChild(sprite);
	sprite.setPosition(this.size.width / 2, this.size.height / 2);
	return true;
}
});

Once the menu scene is loaded, i am not able to press the back button. Nothing happens if i press the back button.

Two things that stand out when looking at your code.

In the gamelayer you’re not using new when creating a new sprite.
in the gameLayer you’re accessing this.size which is a private variable on the device (it’ll work in the browser), so you need to use this.getSize().width

When you want to debug these kind of things on an android device try to put some log messages in your code to determine where the execution holds, whenever you don’t get a clear error message.

I was using the browser to debug and i found no errors.
Thanks for advice. I just found that we can tract the error in android using logcat.

Thank you.

@araker

** If i create a new app and try to run it in android without modifying anything on the files, the works.
But i am not able to go back by pressing the back button.

Is that normal?

** If a variable is defined with _, is it private variable?

I’ve just tried it in my app and the back button doesn’t work as well. It seems its default behaviour.

Variables with _ are private indeed, they should be accessed with their getter and setter methods.

You have to add this on the onEnter of your scene (or the node you want the listener) to get the backbutton:

		cc.eventManager.addListener({
			event: cc.EventListener.KEYBOARD,
			onKeyReleased: this.onKeyReleased
		}, this);

and this as a function of the scene:

onKeyReleased: function (key, event) {
		var target = event.getCurrentTarget();
		switch(key) {
		case cc.KEY.back: /* code here */
                  break;
		}
		event.stopPropagation();
	}
1 Like

I did not clarify that the onKeyReleased goes in the scene class as an additional function ;D