Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make two identical images move separate of each other?
    primarykey
    data
    text
    <p>so I'm in intro to java and my skills are limited, our teacher wants us to make a screen saver. My goal here is 2 have multiple hot air balloon objects bounce around the screen at the same time, when they hit a wall they change direction randomly. I got one balloon to bounce randomly except that it wigs out sometimes and still goes off the screen, i assume that issue is in my math though.<br> The issue I need help with is that when I add a second image to the applet both images seem linked, they move the exact same and when one changes direction so does the other the only thing that differs is starting coordinates, how do I make them move separate of each other? heres my code.</p> <pre><code>*** import acm.program.*; import acm.graphics.*; import java.awt.Color; public class HotAirBalloons extends GraphicsProgram { private static final int APPLET_WIDTH = 800; private static final int APPLET_HEIGHT = 600; private int speedX = 1; private int speedY = 1; public void init() { setSize(APPLET_WIDTH,APPLET_HEIGHT); setBackground(new Color(100,210,255)); } public void moveRandomDirection() { double direction = Math.random() * 2.0 * Math.PI; double speed = 3.0; speedX = (int) (speed * Math.cos(direction)); speedY = (int) (speed * Math.sin(direction)); } public void run() { GImage img1 = new GImage("balloon.jpg"); add(img1, 0, 0); GImage img2 = new GImage("balloon.jpg"); add(img2, 200, 200); while(true) { pause(15); img1.move(speedX, speedY); img2.move(speedX, speedY); if (img1.getX() &gt; APPLET_WIDTH - 50) { moveRandomDirection(); } if (img1.getX() &lt; 1) { moveRandomDirection(); } if (img1.getY() +85 &gt; APPLET_HEIGHT) { moveRandomDirection(); } if (img1.getY() &lt; 1) { moveRandomDirection(); } if (img2.getX() &gt; APPLET_WIDTH - 50) { moveRandomDirection(); } if (img2.getX() &lt; 1) { moveRandomDirection(); } if (img2.getY() +85 &gt; APPLET_HEIGHT) { moveRandomDirection(); } if (img2.getY() &lt; 1) { moveRandomDirection(); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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