2011-09-05 33 views
0

我有一个箭头和一个球。箭头的anchorPoint位于底部。我需要做的就是使精灵能够随着触摸旋转,然后线性冲动施加到球上并沿箭头指向的方向射击。应用线性脉冲旋转Sprite-Cocos2d/Box2D

E.G.

假设您将箭头旋转70度,然后按开火按钮。球然后以70度角射门。

目前,这是我在用,但球不中箭头所指过的方向拍摄。

触摸旋转箭头。

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    //acquire the previous touch location 
    CGPoint firstLocation = [touch previousLocationInView:[touch view]]; 
    CGPoint location = [touch locationInView:[touch view]]; 

    //preform all the same basic rig on both the current touch and previous touch 
    CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location]; 
    CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation]; 

    CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position); 
    CGFloat firstRotateAngle = -ccpToAngle(firstVector); 
    CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle); 

    CGPoint vector = ccpSub(touchingPoint, _arrow.position); 
    CGFloat rotateAngle = -ccpToAngle(vector); 
    CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle); 

    //keep adding the difference of the two angles to the dial rotation 
    arrowRotation += currentTouch - previousTouch; 
} 

火按钮以箭头指向的方向射击球。

-(void) fireBall: (id)sender 
{ 
    [self unscheduleUpdate]; 

    direction = arrowRotation; 
    b2Vec2 force = b2Vec2(direction, 24.8); 
    _ballBody->ApplyLinearImpulse(force, _ballBody->GetWorldCenter()); 
} 

任何帮助,非常感谢! 在此先感谢!

回答

1

你申请到球的冲动(direction, 24.8)。不应该是:(direction.x * 24.8, direction.y * 24.8)。这可能不是唯一的问题,但它是跳出我的。

+0

如果我这样做,线性冲动不适用于球。 – Jonathan

+0

你是怎么试图用你当前的代码来试用它的。你的旋转在一分钟不是一个向量,并没有一个x,y在我给你的代码中使用 –

+0

我拥有的代码是我知道旋转精灵的唯一方法。我还在学习。你能不能展示一些示例代码? – Jonathan