Maybe cause you’re not calling MainLayer’s init() that it inherited from cc.Layer? It’s just a guess, so try doing MainLayer.init() after you new it or better yet, create a static create function that both news and calls init (which is done throughout cocos2d).
Nope… same behavior….
It’s just weird that When I call (from main.js) MainLayer.sceneWithSelf it works and when I call MainLayer.sceneWithSelf() it doesnt…. but If I call MainLayer.sceneWithSelf() within the MainLayer.js files file it DOES works… weird…
Without more context I’m really not sure.
It might have to do when you are calling it, if you are invoking the function too early (before the cocos2d-html5 library has loaded) then that could cause it.
What I don’t understand is how MainLayer.SceneWithSelf (without the “()”) works since that just equals a function, doesn’t invoke it.
If you want to share main.js and such, that might help.
this is my main.js:
var cocos2dApp = cc.Application.extend({
>
config:document[‘ccConfig’],
>
ctor:function (scene) {
this.*super;
this.startScene = scene;
cc.COCOS2D_DEBUG = this.config;
cc.initDebugSetting;
cc.setup;
cc.AppController.shareAppController.didFinishLaunchingWithOptions;
},
>
applicationDidFinishLaunching:function {
// initialize director
var director = cc.Director.getInstance;
>
var screenSize = cc.EGLView.getInstance.getFrameSize;
var resourceSize = cc.size;
var designSize = cc.size;
>
var searchPaths = ;
>
cc.FileUtils.getInstance.setSearchResolutionsOrder;
>
director.setContentScaleFactor;
>
cc.EGLView.getInstance.setDesignResolutionSize;
>
// turn on display FPS
director.setDisplayStats;
>
// set FPS. the default value is 1.0/60 if you don’t call this
director.setAnimationInterval;
>
director = cc.Director.getInstance;
winSize = director.getWinSize;
centerPos = cc.p;
>
//load resources
cc.LoaderScene.preload{
director.replaceScene);
}, this);
>
return true;
}
});
>
var myApp = new cocos2dApp;
and this is my MainLayer.js
var MainLayer = cc.Layer.extend{
this.*super();
>
var about = cc.LabelTTF.create(“Hello MainLayer!”, “Arial”, 28, cc.SizeZero(), cc.TEXT_ALIGNMENT_LEFT );
about.setPosition(winSize.width / 2, winSize.height/2 );
about.setColor(cc.c3b(255,255,255));
this.addChild(about);
>
return this;
},
>
onEnter:function () {
>
this._super();
if ( ‘touches’ in sys.capabilities ) {
this.setTouchEnabled(true);
}
>
if ( ‘mouse’ in sys.capabilities ) {
this.setMouseEnabled(true);
}
>
//Schedules update:function(dt);
//this.scheduleUpdate();
},
update:function (dt) {
cc.log(“update” + dt);
}
});
>
MainLayer.sceneWithSelf = function(){
>
var scene = cc.Scene.create();
var layer = new MainLayer();
scene.addChild(layer);
return scene;
};
the funny thing is if I call MainLayer.sceneWithSelf() within the MainLayer.js file it works! (and if I dont use () it doesnt work!)
and on main.js it will only work if I dont use ()….
this is pretty weird… Iam just begning with javascript so I have no clue why that happens…
Ahh that makes sense now!
var myApp = new cocos2dApp(MainLayer.sceneWithSelf);
As your main.js is currently set up, the cocos2dApp expects a scene’s constructor function to invoke after it has loaded all of the resources. When you give it the ’()’s then you’re invoking your function which is trying to create the scene and layer before such important things like the Director being set up is complete.
You can change main.js to better fit the scheme you’ve created.
Change the above line to the one below:
var myApp = new cocos2dApp(MainLayer);
And change the line:
director.replaceScene(new this.startScene());
To:
director.replaceScene(this.startScene.sceneWithSelf()); // Though maybe change the variable name startScene ;).
If you don’t want to change main.js, then you should create your own scene class and have it do it’s work in the constructor function.
Ohhhhhhhhhh I seee it now Thanks so much again C.J. you are of great help I am just getting started with cocos2d-js and these little things were driving me crazy
I won’t touch the code, I just wanted to understand what was going on.
Thanks again