2

我想知道是否有人知道我将如何将 a 附加CCSprite到 acpConstraint并使其与物理更新。这是我的代码:

upper = [game.spaceManager addPolyAt:cpv(70,195) mass:300 rotation:0 numPoints:6 points:cpv(2,12), cpv(28,8), cpv(33,0), cpv(36,-10), cpv(-33,-10), cpv(-20,8)];
upper->collision_type = kTireCollisionType;

cpShape *lower = [game.spaceManager addPolyAt:cpv(70,125) mass:300 rotation:0 numPoints:7 points:cpv(34,8), cpv(31,0), cpv(25,-9), cpv(7,-13), cpv(-20,-8), cpv(-30,0), cpv(-35,8)];
lower->collision_type = kTireCollisionType;

//HIT POINT
cpShape *sensor = [game.spaceManager addCircleAt:cpv(70,160) mass:10 radius:10];
sensor->sensor = YES;
sensor->collision_type = kScoreCollisionType;

cpShape *sensor2 = [game.spaceManager addCircleAt:cpv(70, 160) mass:10 radius:10];
sensor2->sensor = YES;

//Combine them into one body!
[game.spaceManager combineShapes:upper, lower, sensor, sensor2, nil];

//The "rope"
cpVect a1 = cpv(0,30);      //Local coordinates of tire
cpVect a2 = cpv(70,320);    //World coordinates (staticBody is at (0,0))

//calculate the length of the rope
float max = cpvdist(cpBodyLocal2World(upper->body, a1), a2);
cpConstraint *rope = [game.spaceManager addSlideToBody:upper->body fromBody:game.spaceManager.staticBody toBodyAnchor:a1 fromBodyAnchor:a2 minLength:1 maxLength:max];

cpConstraintNode *ropeNode = [cpConstraintNode nodeWithConstraint:rope];
ropeNode.color = ccBLUE;
[game addChild:ropeNode];

//Attach a sprite to the sensor, lucky for us it's exactly in the center
//of the tire... otherwise this wouldn't work
[game addChild:[super initWithShape:sensor file:@"TractorTireFront.png"] z:3];
[game addChild:[cpCCSprite spriteWithShape:sensor2 file:@"TractorTireBack.png"] z:1];

ivGame = game;

//Free the shape when we are released
self.spaceManager = game.spaceManager;
self.autoFreeShape = YES;

[self schedule:@selector(step:) interval:.1];

return self;

这是该init方法的代码。我在屏幕上有一个轮胎,你可以看到有cpConstraint一根绳子,它从屏幕顶部悬挂并连接到轮胎顶部。我想以某种方式将 a 附加CCSprite到这个约束上,让它随着轮胎的摆动而更新,或者如果有另一种使用 a 的方法CCSprite,让它随着轮胎的摆动而更新。

我正在使用 cocos2d + spaceManager。我听说人们说只使用 aCCSprite并使用轮胎运动来更新它,但我对 cocos2d 或 spaceManger 不够熟悉来实现这一点,所以如果有人有一些代码片段或教程向我展示我该如何去做,我非常感谢。

4

1 回答 1

0

我不知道花栗鼠,但你会在你的蜱虫方法中加入的是

-(void) tick() {

// not sure how you get position in chipmunk, somehow convert it to cgPoint
CGPoint * p = ccp(cpConstraint->position.x, cpConstraint->position.y);

//find out the offset of the tire compared to the cpConstraint then add it to p

p = ccp(p.x + offset.x, p.y + offset.y);

//assign it to your CCSprite

sprite.position = p;

}

初始化 CCSprite

CCSprite * s = [CCSprite spritreWithFile:"picture.png"];

//add to CCLayer with is self here

[self addChild:s];
于 2012-08-29T08:41:45.817 回答