Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy won't player 2's move method work without code for player 1?
    text
    copied!<p>//I am trying to do some edits on player 2's code, so I removed everything involving player 1. But for some reason player 2 does not do anything at all without the code for player 1 in place.(it is supposed to move using the keys i, j, k and l.) So my question is "How can I get the move method for player 2 to work without the code for player1?" Also this is the best SSCCE I could provide. I removed any code that does not involve the creation of the JFrame and player 2. I also kept the code responsible for player 2's movement. </p> <pre><code>import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class Collision extends JFrame { </code></pre> <p>//Jframe</p> <pre><code> final int WIDTH = 900, HEIGHT = 650; double p1Speed = .5, p2Speed = .5; final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3; int p1Direction = UP; int p2Direction = UP; </code></pre> <p>//Code that creates player 2</p> <pre><code>Rectangle p2 = new Rectangle(((WIDTH / 9) + ((int) ((WIDTH / 9) * 1.5) / 2)), (HEIGHT / 2) + (HEIGHT / 10), WIDTH / 30, WIDTH / 30); public Collision() { super("Radical Racing"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); Move2 m2 = new Move2(); m2.start(); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.DARK_GRAY); g.fillRect(0, 0, WIDTH, HEIGHT); g.setColor(Color.GREEN); g.setColor(Color.red); g.fill3DRect(p2.x, p2.y, p2.width, p2.height, true); } </code></pre> <p>//This is p2 move method. I thought my error would be here but Iv'e copied this out of a book exactly. </p> <pre><code>private class Move2 extends Thread implements KeyListener { public void run() { addKeyListener(this); while (true) { try { repaint(); //increase speed a bit if (p2Speed &lt;= 5) { p2Speed = .2; } </code></pre> <p>//these will move the player based off direction</p> <pre><code> if (p2Direction == UP) { p2.y -= (int) p2Speed; } if (p2Direction == DOWN) { p2.y += (int) p2Speed; } if (p2Direction == LEFT) { p2.x -= (int) p2Speed; } if (p2Direction == RIGHT) { p2.x += (int) p2Speed; } Thread.sleep(75); } catch (Exception e) { } } } //you must also implement this method from key listener public void keyPressed(KeyEvent event) { } public void keyReleased(KeyEvent event) { } public void keyTyped(KeyEvent event) { if (event.getKeyChar() == 'j') { p2Direction = LEFT; } if (event.getKeyChar() == 'k') { p2Direction = DOWN; } if (event.getKeyChar() == 'l') { p2Direction = RIGHT; } if (event.getKeyChar() == 'i') { p2Direction = UP; } } } //This starts the program by calling the constructor: public static void main(String[] args) { new Collision(); } } </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