1

I have 22 images... 11 are (rightwall1, rightwall2, rightwall3, etc) the other 11 are (leftwall1, leftwall2, leftwall3, etc)

I am placing each one ontop of the other by setting their y value to the previous walls y value plus the height of the wall (all walls are the same height).

This works fine!

Now, I was trying to set the x value using arc4random so they would jump all over the place in a certain range...

The right walls work perfectly, but the left walls, some of them work but most of them don't show up on the screen.

I manually set the x value of one to -15 and it showed up on the screen and that is lower than the arc4random allows it to go.

The only problem I can think of is i'm subtracting wrong for the left walls?

Here is the code to set the walls locations, I have no other code in the whole project yet.

-(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)) );

}
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195

1 Answers1

0

I manually set the x value of one to -15 and it showed up on the screen and that is lower than the arc4random allows it to go.

((arc4random()%75)-60) returns numbers between -60 and 14, since (arc4random()%75) returns numbers between 0 and 74.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • OHHHHHHHHHHHHHHHH! HAHAHHAHAHAHAHAHHAHAHAAH – Albert Renshaw May 30 '11 at 17:39
  • Still didn't work though, the problem is when dealing with negative arc4random values you can't use integers you have to use floats.. If subtracting you can't just subtract you have to add the arc4random times a negative 1 float value. This is because arc4random uses modulus I assume... – Albert Renshaw May 31 '11 at 04:52
  • arc4random returns unsigned values. Using % limits the range. You don't need floats -- just signed integers after %. – Lou Franco May 31 '11 at 11:53