对不起,我是新手。现在我正在尝试创建一个无限随机移动的精灵,它会向它的方向旋转。但是我不知道如何在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]
];