0
votes

I'm trying to implement chipmunk physics engine for a cocos2d-js game. I'm getting the following error when i run it.

jsb: ERROR: File Y:\Documents\cocos\PrebuiltRuntimeJs\frameworks\js-bindings\bindings\auto\jsb_cocos2dx_auto.cpp: Line: 2143, Function: js_cocos2dx_Node_setPhysicsBody
Invalid Native Object
JS: D:/PROJECTS/cocos/Sliderule/runtime/win32/../../src/app.js:32:Error: Invalid Native Object 


Here is the code i'm working with

`init:function () {
        this._super();
        var size = cc.winSize;
        this.rect1 = new cc.Sprite(res.null_png,cc.rect(0,0, 200, 25));
        this.rect1.setColor(cc.color(255,50,50,1));
        this.rect1.setPosition(size.width/2, size.height-12.5);
        this.rect1._setAnchorX(0.5);
        this.rect1._setAnchorY(0.5);

        this.rectbody1 = new cp.Body(1,cp.momentForBox(1,this.rect1.getContentSize().width, this.rect1.getContentSize().height));
        this.rectbody1.p = cc.p(size.width/2, size.height-12.5);        
        this.space.addBody(this.rectbody1);
        this.rectshape1 = new cp.BoxShape(this.rectbody1, this.rect1.getContentSize().width - 14, this.rect1.getContentSize().height);
        this.space.addShape(this.rectshape1);
        this.rect1.setPhysicsBody(this.rectbody1);
        this.addChild(this.rect1,1);
`

I get the problem when setting the body to the sprite. Thanks in Advance.

1

1 Answers

2
votes

This error message usually appears because of a missing retain(). You have to explicitly set sprites to be kept by the native system (Android, iOS) otherwise it's not valid after some time. And then, if you don't need it anymore: release it.

Try:

this.rect1.retain()

after you created the sprite. And then

this.rect1.release()

when you don't need it anymore.