I am testing out switching scenes. I created a very simply start screen with a game title and one button that the player will press to switch to the main scene.
The start screen:
var StartScreenLayer = cc.Layer.extend({
sprite: null,
ctor: function () {
this._super();
var size = cc.winSize;
var pos = [480, 360];
ship = CreateSprite("res/StartScreen.png", pos[0], pos[1], this);
start_Button = CreateSprite("res/Button_Fire.png", 65, 60, this);
start_Button.setTag(101);
cc.eventManager.addListener(listener_TouchStartScreen, start_Button);
return true;
},
});
In the listener added to the start screen, I have the following to switch the scene:
onTouchEnded: function (touch, event) {
var target = event.getCurrentTarget();
switch (target.getTag()) {
case 101:
var scene = cc.Scene.create();
var layer = new MainGameLayer();
scene.addChild(layer);
cc.Director.getInstance().replaceScene(scene);
break;
};
return true;
}
unfortunately, the main scene crashes when I attempt to add the main scene listener:
from my undestanding this sort of error occurs when an object is released natively, but I can’t figure out which object would have been released. Any help would be appreciated. Thanks
You are correct that Invalid Native Object means it has been released natively. This often happens when people use the create method for an Cocos object and don’t add it to the scene or retain it.
What is your code for creating listener_Keyboard and mainscene.node? If one or both of those were not added to your scene before the function they were created in ended, it’s likely that is your problem.
My guess is that because you don’t immediately add listener_Keyboard to the event manager, or retain it, it is being freed before you try to use it. If you set this scene as the active scene on start then it is probably added to the event manager before it has a chance to be released.
You either need to call listener_Keyboard.retain() immediately after creating it, or you need to create the listener inside MainGameLayer's ctor instead.
retaining the listener_Keyboard fixes this for Windows desktop and Android. Web still doesn’t process, but I’ll keep poking around to see if I can figure out what I missed. thanks
onTouchEnded: function (touch, event) {
var scene = cc.Scene.create();
var layer = new MainGameLayer();
scene.addChild(layer);
cc.Director.getInstance().replaceScene(scene); // fails here
return true;
}
Moving the listener creation into the ctor yields the same result
In onTouchEnded, if you comment out the line making the new MainGameLayer and comment out the line adding the layer to the scene, does it change scene?
i.e.
onTouchEnded: function (touch, event) {
var scene = cc.Scene.create();
// var layer = new MainGameLayer();
// scene.addChild(layer);
cc.Director.getInstance().replaceScene(scene);
return true;
}
Ok, I cleared my browser cache and am getting the same behavior, but I now get the following error in the Browser Console when I attempt to switch scenes:
TypeError: cc.Director.getInstance is not a function
onTouchEnded: function (touch, event) {
var scene = cc.Scene.create();
var layer = new MainGameLayer();
scene.addChild(layer);
cc.Director.getInstance().replaceScene(scene); // fails here
return true;
}
with
onTouchEnded: function (touch, event) {
var scene = cc.Scene.create();
var layer = new MainGameLayer();
scene.addChild(layer);
cc.director.runScene(scene);
return true;
}
fixes the issue. Not sure why considering the singleton is supported in the documentation