Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're using the same speedX and speedY variable for both of the balloons. Give them their own speed variables.</p> <p>Also, the reason why the balloons "wig out sometimes" is that when you choose a random direction, you don't specify that it can't be in the direction that you're trying to get them to go away from.</p> <p>To fix that issue, I'd suggest that you create a moveRandomDirection method for each balloon, an pass in a value that represents which edge they have hit. Then restrict the direction they can move to be any of the other directions.</p> <p>Try this:</p> <pre><code>public void moveBalloonOneInRandomDirection(int whichEdge) { double direction = 0; switch(whichEdge) { case(0): // Top edge direction = Math.random() * Math.PI + ONLY_ALLOW_DOWN_VAL; case(1): // Left edge direction = Math.random() * Math.PI + ONLY_ALLOW_RIGHT_VAL; case(2): // Bottom edge direction = Math.random() * Math.PI + ONLY_ALLOW_UP_VAL; case(3): // Right edge direction = Math.random() * Math.PI + ONLY_ALLOW_LEFT_VAL; } double direction = Math.random() * 2.0 * Math.PI; double speed = 3.0; speedX1 = (int) (speed * Math.cos(direction)); speedY1 = (int) (speed * Math.sin(direction)); } </code></pre> <p>Then in your if statements, you would have something like this:</p> <pre><code>if (img1.getX() &gt; APPLET_WIDTH - 50) // Right edge { moveBalloonOneInRandomDirection(3); } </code></pre> <p>You should probably also use constants, so that instead of the "magic numbers", you would have </p> <pre><code>private static final int RIGHT_EDGE = 3; ... moveBalloonOneInRandomDirection(RIGHT_EDGE); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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