使用cocos 2d js和Chipmunk创建Dynamic Body

时间:2015-04-26 21:56:13

标签: javascript cocos2d-x game-physics chipmunk cocos2d-js

我对cocos 2d和花栗鼠相当新。到目前为止,我已设法创建一个静态对象并为其添加冲突处理程序。但我想创建一个动态的.`this.space = space;         this.sprite = new cc.PhysicsSprite("#rock.png");         var body = new cp.StaticBody();         body.setPos(POS);         this.sprite.setBody(主体);

    this.shape = new cp.BoxShape(body,
        this.sprite.getContentSize().width,
        this.sprite.getContentSize().height);
    this.shape.setCollisionType(SpriteTag.rock);

    this.space.addStaticShape(this.shape);
    spriteSheet.addChild(this.sprite);`

但我想创建一个类似动态的身体,例如:

cp.v(310, 0), cp.v(0, 0)

我知道这是非常基本的,但如果有人能帮助我,我会很感激。如果你在JS中有cocos 2d和chipmunk的良好文档。分享吧。感谢

1 个答案:

答案 0 :(得分:1)

你走了:

//Add the Chipmunk Physics space
var space = new cp.Space();
space.gravity = cp.v(0, -10);

//Optionally add the debug layer that shows the shapes in the space moving:
/*var debugNode = new cc.PhysicsDebugNode(space);
debugNode.visible = true;
this.addChild(debugNode);*/

//add a floor:
var floor = new cp.SegmentShape(this.space.staticBody, cp.v(-1000, 10), cp.v(1000, 0), 10);
//floor.setElasticity(1);
//floor.setFriction(0);
space.addStaticShape(floor);

//add a square to bounce
//the Sprite
var mySprite = cc.PhysicsSprite.create("res/something.png");
gameLayer.addChild(mySprite);

//the Body
var size = mySprite.getContentSize();
var innertialMomentum = 1; //Use Infinity if you want to avoid the body from rotating
var myBody = new cp.Body(innertialMomentum , cp.momentForBox(innertialMomentum , size.width, size.height));
mySprite.setBody(myBody);
space.addBody(myBody);
//myBody.p = cc.p(xxx, yyy); //To alter the position of the sprite you have to manipulate the body directly, otherwise it won't have the desired effect. You can also access it by mySprite.body

//the Shape
var myShape = new cp.BoxShape(myBody, size.width, size.height);
//myShape.setElasticity(1);
//myShape.setFriction(0);
space.addShape(myShape);

//Apply your desired impulse
//mySprite.body.applyImpulse(cp.v(ix,iy), cp.v(rx,ry)); // Where the first vector is for the impulse strength and direction and the second is for the offset between the center of the object and where you want the impulse to be applied.