MoonWarriors - Javascript question

Hi,

I’m reading the code of MoonWarriors to learn how to use JS to write a Cocos2D-X game sandbox; Sorry if this question is more about JS than CC2D-x!

As I open GameLayer for instance, I see that functions can be added in three different ways. I try to understand the added value of each approach…

First approach I see is in the extend parameter:
`var GameLayer = cc.Layer.extend({
processEvent:function( event ) {
//…
},

// …
}`

The second is on that GameLayer (global?) variable:
GameLayer.create = function () { //...
Is this like creating a Class method in Java - Using a common set of data whatever the calling instance is?
BTW, Is this GameLayer a variable/instance, a “class”?

Third approach is about adding “stuff” the the prototype:
GameLayer.prototype.addEnemy = function (enemy,z,tag){

Am I right to understand this is like an instance method in Java or ObjC?

Thanks…
J.

1st,3ird way, it is member function
2end way, it is static function

you could try to learn javascript basic content first

Hi,

Thanks for your answer, the clarification is very helpful. I’ve always been using the verbose prototype approach and got confused by seeing all 3 different approaches.

Now I see that Sprite extends cc.Class.
What framework / source code handles the extend method in the case of CC2D-x? And where is that cc.Class defined pleaase?

Thanks,
J.

in cocos2d-x src/cocos2d/platform/CCClass.js, you could see how to implement the extend method.
you could search any keyword in windows box easily, the file almost start with “CC” prefix

Hey thanks a lot Huang, you’ve helped me well on this!

Have a good one,
J.