对不起,我是新来的。 现在我正在尝试创建一个无限随机移动的精灵,旋转到它的方向。但是我无法弄清楚如何在rotateAction上使用randomPoint生成的随机位置。基本上这个bug随机旋转而不是使用它要去的点。有没有办法可以两次使用相同的随机点?
-(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]
];
答案 0 :(得分:0)
有些突出的事情:看起来你正试图让角度走向错误的方向。不需要条件偏移。应该否定弧度到度数。我猜你想要:
float dY = randomPoint.y - roach.position.y;
float dX = randomPoint.x - roach.position.x;
float angle = -CC_RADIANS_TO_DEGREES(atan2f(dY, dX));