0

申し訳ありませんが、私はこれが初めてです。現在、その方向に回転する無限のランダムに移動するスプライトを作成しようとしています。ただし、rotateAction で randomPoint から生成されたランダムな位置を使用する方法がわかりません。基本的に、バグは移動先のポイントを使用する代わりにランダムに回転します。同じランダム ポイントを 2 回使用する方法はありますか?

-(void)moveRandom:(CCSprite*)roach
{

    CGPoint randomPoint = ccp(arc4random()%480, arc4random()%320);
    NSLog(@"%@", NSStringFromCGPoint(randomPoint));

    int minDuration = 2.0;
    int maxDuration = 4.0;
    int rangeDuration = maxDuration - minDuration;
    int randomDuration = (arc4random() % rangeDuration) + minDuration;

    float dY = roach.position.y - randomPoint.y;
    float dX = roach.position.x - randomPoint.x;
    float offset = dX<0 ? 90.0f : -90.0f;
    float angle = CC_RADIANS_TO_DEGREES(atan2f(dY, dX)) + offset;

    [roach runAction:
     [CCActionSequence actions:
      [CCActionRotateTo actionWithDuration:0.001 angle:angle],
      [CCActionMoveTo actionWithDuration:randomDuration position: randomPoint],
      [CCActionCallBlock actionWithBlock:^{
         [self performSelector:@selector(moveRandom:) withObject:roach afterDelay:0.5];
     }],
      nil]
     ];
4

1 に答える 1

0

目立つ点: 角度を間違った方向に向けようとしているようです。条件付きオフセットは必要ありません。ラジアンから度は否定されるべきです..私はあなたが望むと思います:

float dY = randomPoint.y - roach.position.y;
float dX = randomPoint.x - roach.position.x;
float angle = -CC_RADIANS_TO_DEGREES(atan2f(dY, dX));
于 2014-12-05T17:36:46.187 に答える