xcode CGPoint arc4random无法正常工作

时间:2011-05-30 15:26:55

标签: iphone xcode cgpoint arc4random xvalue

我有22个图像... 11个(右墙1,右墙2,右墙3等),其他11个(左墙1,左墙2,左墙3等)

我通过将y值设置为前一个墙y值加上墙的高度(所有墙都是相同的高度)来将每个人放在另一个上面。

这很好用!

现在,我试图使用arc4random设置x值,这样它们就会在某个范围内跳到那个地方......

右侧墙壁工作得很好,但左侧墙壁,其中一些可以工作,但大多数都不会出现在屏幕上。

我手动将1的x值设置为-15并且它显示在屏幕上,并且低于arc4random允许它去的位置。

我能想到的唯一问题是我的左墙减去了错误吗?

以下是设置墙壁位置的代码,我在整个项目中还没有其他代码。

-(void)awakeFromNib {

    rightWall1.center = CGPointMake( ((arc4random()%75)+330), (460-((rightWall1.frame.size.height)/2)) );//460 = bottom of screen
    rightWall2.center = CGPointMake( ((arc4random()%75)+330), ((rightWall1.center.y)-(rightWall2.frame.size.height)) );
    rightWall3.center = CGPointMake( ((arc4random()%75)+330), ((rightWall2.center.y)-(rightWall3.frame.size.height)) );
    rightWall4.center = CGPointMake( ((arc4random()%75)+330), ((rightWall3.center.y)-(rightWall4.frame.size.height)) );
    rightWall5.center = CGPointMake( ((arc4random()%75)+330), ((rightWall4.center.y)-(rightWall5.frame.size.height)) );
    rightWall6.center = CGPointMake( ((arc4random()%75)+330), ((rightWall5.center.y)-(rightWall6.frame.size.height)) );
    rightWall7.center = CGPointMake( ((arc4random()%75)+330), ((rightWall6.center.y)-(rightWall7.frame.size.height)) );
    rightWall8.center = CGPointMake( ((arc4random()%75)+330), ((rightWall7.center.y)-(rightWall8.frame.size.height)) );
    rightWall9.center = CGPointMake( ((arc4random()%75)+330), ((rightWall8.center.y)-(rightWall9.frame.size.height)) );
    rightWall10.center = CGPointMake( ((arc4random()%75)+330), ((rightWall9.center.y)-(rightWall10.frame.size.height)) );
    rightWall11.center = CGPointMake( ((arc4random()%75)+330), ((rightWall10.center.y)-(rightWall11.frame.size.height)) );


    leftWall1.center = CGPointMake( ((arc4random()%75)-60), (460-((leftWall1.frame.size.height)/2)) );
    leftWall2.center = CGPointMake( ((arc4random()%75)-60), ((leftWall1.center.y)-(leftWall2.frame.size.height)) );
    leftWall3.center = CGPointMake( ((arc4random()%75)-60), ((leftWall2.center.y)-(leftWall3.frame.size.height)) );
    leftWall4.center = CGPointMake( ((arc4random()%75)-60), ((leftWall3.center.y)-(leftWall4.frame.size.height)) );
    leftWall5.center = CGPointMake( ((arc4random()%75)-60), ((leftWall4.center.y)-(leftWall5.frame.size.height)) );
    leftWall6.center = CGPointMake( ((arc4random()%75)-60), ((leftWall5.center.y)-(leftWall6.frame.size.height)) );
    leftWall7.center = CGPointMake( ((arc4random()%75)-60), ((leftWall6.center.y)-(leftWall7.frame.size.height)) );
    leftWall8.center = CGPointMake( ((arc4random()%75)-60), ((leftWall7.center.y)-(leftWall8.frame.size.height)) );
    leftWall9.center = CGPointMake( ((arc4random()%75)-60), ((leftWall8.center.y)-(leftWall9.frame.size.height)) );
    leftWall10.center = CGPointMake( ((arc4random()%75)-60), ((leftWall9.center.y)-(leftWall10.frame.size.height)) );
    leftWall11.center = CGPointMake( ((arc4random()%75)-60), ((leftWall10.center.y)-(leftWall11.frame.size.height)) );

}

1 个答案:

答案 0 :(得分:0)

  

我手动将1的x值设置为-15并且它显示在屏幕上,并且低于arc4random允许它去的位置。

((arc4random()%75)-60)返回-60到14之间的数字,因为(arc4random()%75)返回0到74之间的数字。