Helper function to create a scene from a layer

I have the following in obj-c and I would like to make the js version of it:

  • (CCScene**)sceneWithNode:node{
    CCScene**scene = [CCScene node];
    [scene addChild: node];
    return scene;
    }

just extending my question: how can I subclass cc.Layer and create a (static?) method that would return the layer itself inside a cc.Scene??

Hopefully this is right, I don’t know obj-c ;).

MyGame = {};

MyGame.sceneWithNode = function(node) {
    var scene = cc.Scene.create();
    scene.addChild(node);
    return scene;
}

//
// Other question
//

MyGame.MyLayer = cc.Layer.extend({
    // My members!
    _member1: 0,
    _member2: 1,

    // My functions!
    ctor: function() {
        // My constructor!
        this._super();
    },

    init: function() {
        return this._super();
    },

    func1: function() {
        return this._member1;
    },

    func2: function() {
        return this._member2;
    }
});

MyGame.MyLayer.create = function(){
    var layer = new MyGame.MyLayer();

    if (!layer.init()) {
        return null;
    }

    return layer;
};

MyGame.MyLayer.sceneWithSelf = function() {
    // I'm assuming that by a 'static method that will return itself' you mean create
    // itself and add it to a scene.
    return MyGame.sceneWithNode(MyGame.MyLayer.create());
};

Let me know if I didn’t understand correctly and I’ll update my example :).

Thanks for the response C.J. Kimberlin, it has helped me, I just need one more thing to do what I want:

So I have this “class” BaseLayer:

var BaseLayer = cc.Layer.extend({
>
ctor:function() {
this.*super;
cc.associateWithNative;
},
>
init:function{
>
this.*super();
var about = cc.LabelTTF.create(“Hello Cocos”, “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;
}
});
>
BaseLayer.create = function(){
>
var layer = new BaseLayer();
if (!layer.init()){
return null;
}
return layer;
};
>
BaseLayer.sceneWithSelf = function(){
>
var scene = cc.Scene.create();
var self = BaseLayer.create();
scene.addChild(self);
return scene;
};

Which works fine when I call BaseLayer.sceneWithSelf
But what I want to achieve is to subclass BaseLayer and be able to call sceneWithSelf… for example:

var MainLayer = BaseLayer.extend({
// stuff of main layer….
)};

And then be able to call MainLayer.sceneWithSelf

How can I do that?

I think with the inheritance mechanism that cocos2d-html5 uses then that pattern won’t work. I believe you can just add sceneWithSelf to the list of values given to extend and don’t leverage ‘this’ in it (I’m not positive though, I’m still relatively new to javascript). Another option is to potentially mess with the prototype member of BaseLayer, that’s when things get interesting ;).

Give the former a shot and see if it solves your issue.

I can’t get it to work….
This is such an easy thing to do in the OO world…. Maybe I have to take another approach to do something like that with javascript…

Yea, it appears that effectively you can’t inherit static functions using the cocos2d inheritance mechanism. So you’ll either want to recreate it or make it not ‘static’.

The more I think about this, the more I think this behavior is appropriate. In the example you gave you would call MainLayer.sceneWithSelf and you’d get back a BaseLayer…not a MainLayer, so you might as well have just called BaseLayer.sceneWithSelf. Actually I can’t come up with a good reason why I would ever need to inherit a static function o.O.

Yea… I guess for now I will just add the sceneWithSelf method to every layer I create …

But thank you very much C.J. :slight_smile:

I’d suggest having a static create() for each layer (which is the cocos2d model anyways) and having a member method that is sceneWithSelf…Then you’d just need to do scene.addChild(this) and it’ll be inherited down.

I will give it a try when I get home !