New issue
 Projects > js > Issues > Bug #5169

cc.PhysicsSprite.extend的物理精灵会报如下的错

Bug #5169 [Rejected]
watson.wan@hotmail.com 2014-05-13 14:35 . Updated over 5 years ago

通过 cc.PhysicsSprite.extend的物理精灵会报如下的错(其实我有setBody)
JS: assets/jsb_cocos2d_extension.js:194:TypeError: body is null

代码如下

var ball = cc.PhysicsSprite.extend({    
    ctor : function(target ,pos,polyVerts,image,collisionType){
        this._super();
        this.initWithFile(image);  

        var body = new cp.Body(1, cp.momentForPoly(0.00001, polyVerts  ,cp.v(0,0) ));
            body.setPos(pos); 
        this.setPosition(pos);        
        var shape = new cp.PolyShape( this.body, polyVerts ,cp.v(0,0) );
            shape.setCollisionType(1);

        target.space.addShape(  shape );         
        target.space.addBody(   body );
        this.setBody(  body ); 

        target.addChild(this);        

        return this;    
    },

    update : function(dt){   

    },
    destroy:function () {

    }    
});
watson.wan@hotmail.com 2014-05-14 01:02

js alpha1 和 js alpha2 打包到手机运行都有这问题,希望beta版本会修正

linshun 2014-06-18 15:08
  • Assignee set to pandamicro
  • Target version set to v3.0 rc
linshun 2014-06-18 15:09

watson: It works properly before v3.0.

pandamicro 2014-06-19 07:32
  • Description updated (diff)
pandamicro 2014-06-19 07:50
var Ball = cc.PhysicsSprite.extend({
    ctor : function(target, pos, polyVerts, image, collisionType){
        this._super(image);

        var body = new cp.Body(1, cp.momentForPoly(0.00001, polyVerts, cp.v(0,0) ));
        body.setPos(pos);
        var shape = new cp.PolyShape( body, polyVerts, cp.v(0,0) );
        shape.setCollisionType(1);

        target.space.addShape( shape );
        target.space.addBody( body );
        this.setBody( body );

        this.setPosition(pos);
        target.addChild(this);

        return this;
    },

    update : function(dt){

    },
    destroy:function () {

    }
});

@watson.wan please try again with this modified class.
In the original code, you have used this.body which have never been defined.

watson.wan@hotmail.com 2014-06-19 15:30

pandamicro:

Sorry , 我这段代码是修改过贴上来的,既然改错了发上来。实际代码是没问题(因为真实代码在2.2.3上运行是OK的),但在cocos2d-js 3上的版本都会报上面的错。

建议在你的电脑上运行类似下面这种代码,看看会不会报我提到的这种错误!!!

** (JS: assets/jsb_cocos2d_extension.js:194:TypeError: body is null)**

    var Ball = cc.PhysicsSprite.extend({
    ctor : function(target, pos, polyVerts, image, collisionType){
        this._super(image);

        var body = new cp.Body(1, cp.momentForPoly(0.00001, polyVerts, cp.v(0,0) ));
        body.setPos(pos);
        var shape = new cp.PolyShape( body, polyVerts, cp.v(0,0) );
        shape.setCollisionType(1);

        target.space.addShape( shape );
        target.space.addBody( body );
        this.setBody( body );

        this.setPosition(pos);
        target.addChild(this);

        return this;
    },

    update : function(dt){

    },
    destroy:function () {

    }
});

非常感谢!!!

pandamicro 2014-06-27 10:07
  • Status changed from New to Rejected
  • % Done changed from 0 to 100

The problem was related to user implementation

Atom PDF

Status:Rejected
Start date:2014-04-29
Priority:Low
Due date:
Assignee:pandamicro
% Done:

100%

Category:-
Target version:v3.0 rc