cocos2d-js:错误:使用setPhysicsBody的无效本机对象

时间:2014-12-29 11:10:53

标签: cocos2d-x cocos2d-x-3.0 chipmunk cocos2d-js

我试图为cocos2d-js游戏实现chipmunk物理引擎。我运行时出现以下错误。

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 


以下是我使用

的代码
`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);
`

将主体设置为精灵时出现问题。在此先感谢。

1 个答案:

答案 0 :(得分:2)

由于缺少retain(),通常会出现此错误消息。您必须显式设置由本机系统(Android,iOS)保留的精灵,否则它在一段时间后无效。然后,如果你不再需要它了:释放它。

尝试:

this.rect1.retain()

创建精灵后。然后

this.rect1.release()

当你不再需要它时。