我正在尝试做游戏。随着块的RandomPositioning我遇到了一些问题。使用我的代码,块可以非常快速地在同一条路上生成。但这不是我的意思。它们看起来像马里奥游戏。我的代码:
HumanMovement = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(HumanMoving) userInfo:nil repeats:YES];
[self PlaceBlocks];
BlockMovement = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(BlockMoving) userInfo:nil repeats:YES];
}
-(void)BlockMoving{
StartingBlock.center = CGPointMake(StartingBlock.center.x -1, StartingBlock.center.y);
Block1.center = CGPointMake(Block1.center.x -1, Block1.center.y);
Block2.center = CGPointMake(Block2.center.x -1, Block2.center.y);
if ( Block1.center.x > 20) {
[self PlaceBlocks];
}
}
-(void)PlaceBlocks{
RandomBlock1Position = arc4random() %568;
RandomBlock1Position = RandomBlock1Position + 140;
RandomBlock2Position = RandomBlock2Position + 500;
Block1.center = CGPointMake(193 , RandomBlock1Position);
Block2.center = CGPointMake(280 , RandomBlock2Position);
}
答案 0 :(得分:0)
所以,如果我理解正确,你的积木一旦到达左侧就应该重生。为此,您必须将中心检查位置更改为:
if ( Block1.center.x < 20) {
[self PlaceBlocks];
}