cc.CallFunc.create causes Native Object error but works in HTML5

cc.CallFunc.create causes Native Object error but works in HTML5
0.0 0


Can someone please give me some help/hint on what is going on here ?

This is a simple count down time before the game starts. all the lblX are TTFLabels

var countDownSpeed = 0.5;
var t = this;
cc.CallFunc.create(function(node) {
t.addChild(lbl3, 500);
lbl3.runAction(cc.ScaleTo.create(countDownSpeed, 0, 0));
}, this),
cc.CallFunc.create(function(node) {
t.addChild(lbl2, 500);
lbl2.runAction(cc.ScaleTo.create(countDownSpeed, 0,0));
}, this),
cc.CallFunc.create(function(node) {
t.addChild(lbl1, 500);
lbl1.runAction(cc.ScaleTo.create(countDownSpeed, 0,0));
}, this),
cc.CallFunc.create(function(node) {
t.addChild(lblGo, 500);
lblGo.runAction(cc.ScaleTo.create(countDownSpeed, 0, 0));
t.schedule(t.gameLogic, spawnRate);
}, this)

cocos2d: jsb: ERROR: File /Users/macuser/SDK/cocos2d-x-3.0alpha1/cocos/scripting/auto-generated/js-bindings/jsb_cocos2dx_auto.cpp: Line: 3233, Function: js_cocos2dx_Node_addChild
cocos2d: Invalid Native Object
cocos2d: JS: /Users/macuser/Library/Application Support/iPhone Simulator/7.0.3/Applications/56FF9F20-FECC-441D-B26D-9709BACAE1F5/ Invalid Native Object


Error: Invalid Native Object
usually means that this js object is referencing its C*+ object, which no longer exists.
why? because Cocos manages memory via Autorelease or reference counting mechanism. Un-used objects will get released the next game cycle .
What happen here is you created this object in JS, C*+ creates an object too.
you stored the JS object somewhere, but C*+ doesn’t know that.
In the next cycle, the C*+ object gets released

How to fix this

Method 1:
manually call `retain() on objects that is not added as a child or not immediately used

but you will have to call `release()@, when you are done with it

Method 2:
add as a child, and set it as not visible


I read what you said above in the manual, but it doesn’t really make sense to me, can you demonstrate it to me by modifying the code above please ?

#4 retain after you create something but add it to a node later
like lb1 =; lb1.retain(); // inc ref count by 1
2.after you really add it to a node ,do release it
like parent.addChild(lb1); lb1.release(); // dec ref count by 1


Thanks Slipper