2

In my app, I have two buttons and a timer that fires an action every second. Each second, it generates four random numbers. The numbers are used as X and Y values of the buttons, and the buttons move to the new points. However, sometimes the new points are close, so the buttons overlap. How can I check to make sure this doesn't happen? I already tried checking if CGRectIntersectsRect of the button frames, but that does nothing.

Thanks for any help!

taskinoor
  • 45,586
  • 12
  • 116
  • 142
JohnWickham
  • 571
  • 1
  • 6
  • 15
  • 2
    Please include your CGRectIntersects checking code. That is probably the best solution, but you may be doing it wrong. – jrturton May 20 '12 at 06:09

1 Answers1

1

Just to verify, your CGRectIntersectsRect code/structure looks somewhat similar to this right:

-(void)update {

// This is the method that gets called every second where your four numbers are randomly generated

// Generate random numbers here
// Position the buttons based on 2 of the random numbers

if (CGRectIntersectsRect(button1.frame, button2.frame) {

 // regenerate the four numbers
 // reposition your buttons
}

Also, I was just wondering, why each second? Maybe perform it on a different thread if you want, and make it 10 times a second so the button overlap does not show for too long, but it depends on how fast you want it I guess.... also why are you generating 4 random numbers if you are only using two (as far as we know).

But, ultimately we need to see your code, otherwise we cannot see what you may be doing wrong.

MCKapur
  • 9,127
  • 9
  • 58
  • 101
  • Yes, your CGRectIntersectsRect code is identical to mine, however it still does not work. I generate four different numbers because one is the X value of button1, the second is the Y value of button1, third is X of button2, four is Y of button 2. – JohnWickham May 20 '12 at 22:19
  • Can you log in the CGRectIntersectsRect ? – MCKapur May 20 '12 at 22:58