I have a CCLayer with CCSprites in it. When I create and add sprites to the layer in init, everything works fine. If I create sprites in init and access them in update, without adding them to the layer, I get an error: “Cocos2d: JS: Invalid Native Object”. The error is reported for the line accessing the given sprite.
Below is example code giving the error:
var SpriteLayer = cc.Layer.extend({
ctor: function() {
this.*super;
cc.associateWithNative;
this.init;
},
init: function {
this.*super();
cc.SpriteFrameCache.getInstance().addSpriteFrames(“spriteSheet.plist”);
this.*sprite = cc.Sprite.createWithSpriteFrameName;
// When this is uncommented this works
//this.addChild;
this.scheduleUpdate;
},
update: function {
this.*sprite.setPosition(cc.p(120,120));
}
});
I am using XCode and the problem appears in simulators and on device. I am using Cocos2d-x v2.1rc0-x-2.1.2 with the hotfix. This works with the HTML5 version.