Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Judging by that code snippet, it looks like you have a single controller which "owns" the ten balls, and you want the balls to bounce around according to a set of rules that are unique to each ball. A more object-oriented approach would be as follows:</p> <pre><code>@interface JumpBallClass { CGPoint center; CGPoint speed; CGPoint lowerLimit; CGPoint upperLimit; } @property (assign) CGPoint lowerLimit; @property (assign) CGPoint upperLimit; - (void)update; @end @implementation JumpBallClass - (void)update { center.x += speed.x; center.y += speed.y; if (center.x &gt; upperLimit.x || center.x &lt; lowerLimit.x) { speed.x = -speed.x; } if (center.y &gt; upperLimit.y || center.y &lt; lowerLimit.y) { speed.y = -speed.y; } } @end </code></pre> <p>This setup would allow you to configure all of the balls once, by setting their upper and lower limits:</p> <pre><code>[jumpBall1 setUpperLimit:CGPointMake(60, 211)]; [jumpBall1 setLowerLimit:CGPointMake(0, 82)]; ... </code></pre> <p>And then simply calling <code>update</code> on each ball in your timer method:</p> <pre><code>- (void) jumpOnTimer { [jumpBall1 update]; [jumpBall2 update]; ... } </code></pre> <p>You can simplify this even further by storing all of the balls in an <code>NSArray</code>:</p> <pre><code>NSArray * balls = [NSArray arrayWithObjects:jumpBall1, jumpBall2, ..., nil]; </code></pre> <p>And then calling <code>makeObjectsPerformSelector:</code></p> <pre><code>[balls makeObjectsPerformSelector:@selector(update)]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload