Search code examples
cocos2d-xcocos2d-html5

cc.Class.extend not respecting functions


When ever I run the code below in Cocos 2D HTML or with bindings it seems to not add any of my functions but will add my Variables.

So:

cc.Class.extend({
   init: function(isDancing){
   this.dancing = isDancing;
},
   age : 5,
   dance: function(){
       return this.dancing;
   }
});

becomes :

 function anonymous() {
   this._super=null;this.age=this.age;
 }

Which I will get a undefined error when I try to call, dance().


Solution

  • constructor function is 'ctor'(not 'init')

    var Klass = cc.Class.extend({
       ctor: function(isDancing){
       this.dancing = isDancing;
    },
       age : 5,
       dance: function(){
           return this.dancing;
       }
    });
    

    undefined

    var el = new Klass(22);
    

    undefined

    el.dance()
    

    22