0

我面临一个问题。我已经完成了一些编码来旋转 cpSegmentShapeNew 但它不起作用。看看下面的代码,

    //**creating shape
testBody = cpBodyNew(INFINITY, INFINITY);
cpShape* testShape = cpSegmentShapeNew(testBody, cpv(230, 82), cpv(193, 46), 0.0f);
testShape->e = 0.0;
testShape->u = 0.0;
testShape->data = flipper;
testShape->collision_type = 2;
cpSpaceAddStaticShape(space, testShape);

//Body moving when user touch
-(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//event that starts when a finger touchs the screen
UITouch *touch = [touches anyObject];
CGPoint tmpLoc = [touch locationInView: [touch view]];
CGPoint location = [[Director sharedDirector] convertCoordinate:tmpLoc];

ball.position = location;
ballBody->p = location;
[flipper runAction:[RotateTo actionWithDuration:0.1f angle:60]];

cpBodySetAngle(testBody, 60);

cpvrotate(testBody->rot, cpv(100000,0));

return kEventHandled;
}

请任何人告诉我我错在哪里。

谢谢。

4

1 回答 1

1

问候,

问题是您正在通过代码旋转两个对象(sprite + body)。

你需要的是旋转一个,让另一个对象知道它已经发生了,所以它也可以这样做。

例如,如果你移动身体,那么更新精灵的方法应该是这样的:

void updateShapes(void* ptr, void* unused)
{
 cpShape* shape = (cpShape*)ptr;
 Sprite* sprite = shape->data;
 if(sprite)
 {
  cpBody* body = shape->body;
  [sprite setPosition:cpv(body->p.x, body->p.y)];
  [sprite setRotation: (float) CC_RADIANS_TO_DEGREES( -body->a )];
 }
}

最后一行代码更新了旋转。那就是您缺少的那条线。

我希望这对您或将来的其他人有所帮助。

祝 cocos2d 伙伴好运!

约翰·T。

于 2009-09-06T22:00:22.390 回答