gpt4 book ai didi

javascript - 使用 cocos 2d js 和 Chipmunk 创建动态主体

转载 作者:行者123 更新时间:2023-11-28 00:22:15 42 4
gpt4 key购买 nike

我对 cocos 2d 和chipmunk 还很陌生。到目前为止,我已经成功创建了一个静态对象并向其添加碰撞处理程序。但我想创造一种动态。

this.space = space;
this.sprite = new cc.PhysicsSprite("#rock.png");
var body = new cp.StaticBody();
body.setPos(pos);
this.sprite.setBody(body);

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 的良好文档。分享吧。谢谢

最佳答案

给你:

//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.

关于javascript - 使用 cocos 2d js 和 Chipmunk 创建动态主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29884055/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com