2

スプライトとタッチ ポイントの間の角度の計算に問題があるようです。ユーザーが画面に触れるたびに、スプライトがタッチポイントの方向を直接向くようにしようとしています。これが私のコードです:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    CGPoint tapPosition;
    for (UITouch *touch in touches){
        CGPoint location = [touch locationInView:[touch view]];
        tapPosition = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:location]];
    }

    float angle = CC_RADIANS_TO_DEGREES(ccpAngle(fish.position, tapPosition));
    [fish runAction:[CCRotateTo actionWithDuration:0.5 angle:angle]];
}

何か案は?ありがとう

4

3 に答える 3

4

これをNikhilの回答の最後に追加して、タッチ位置がスプライトの右下にある場合に負の角度が得られないようにします。

if (calculatedAngle < 0) 
{
     calculatedAngle+=360;
}
于 2012-11-03T04:53:23.000 に答える
3

CCPoint pos1 = [魚の位置]; CCPoint pos2 = タッチロケーション;

float theta = atan((pos1.y-pos2.y)/(pos1.x-pos2.x)) * 180 * 7 /22;

float calculatedAngle;

if(pos1.y - pos2.y > 0)
{
    if(pos1.x - pos2.x < 0)
    {
        calculatedAngle = (-90-theta);
    }
    else if(pos1.x - pos2.x > 0)
    {
       calculatedAngle = (90-theta);
    }       
}
else if(pos1.y - pos2.y < 0)
{
    if(pos1.x - pos2.x < 0)
    {
       calculatedAngle = (270-theta);
    }
    else if(pos1.x - pos2.x > 0)
    {
       calculatedAngle = (90-theta);
    }
}

実行アクションでこのcalculatedAngleを使用してください..これが役立つことを願っています... :)

于 2012-05-18T08:34:33.980 に答える