当用户在cocos2d中触摸屏幕时,如何旋转身体

时间:2009-08-07 05:52:44

标签: iphone cocos2d-iphone

我正面临一个问题。我做了一些编码来旋转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;
}

请有人告诉我,我错了。

感谢。

1 个答案:

答案 0 :(得分:1)

问候,

问题是你是通过代码旋转两个对象(精灵+身体)。

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

例如,如果移动主体,那么更新sprite的方法应如下所示:

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队友!

Yohann T。

相关问题