0

对不起,我是新手。现在我正在尝试创建一个无限随机移动的精灵,它会向它的方向旋转。但是我不知道如何在rotateAction 上使用从randomPoint 生成的随机位置。基本上,错误会随机旋转,而不是使用它要去的点。有没有办法可以两次使用相同的随机点?

-(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 回答