Iphone 当用户在cocos2d中触摸屏幕时,如何旋转身体
我面临一个问题。我已经做了一些编码来旋转cpSegmentShapeNew,但它不起作用。看看下面的代码Iphone 当用户在cocos2d中触摸屏幕时,如何旋转身体,iphone,cocos2d-iphone,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; te
//**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;
}
请任何人告诉我我错在哪里
谢谢 您好
问题是您正在通过代码旋转两个对象(精灵+身体)
你需要的是旋转一个物体,让另一个物体知道它发生了,这样它也可以这样做
例如,如果移动实体,则更新精灵的方法应如下所示:
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 )];
}
}
最后一行代码更新旋转。这就是你错过的那条线
我希望这对你或其他人将来有所帮助
祝你好运,伙计
约汉T