Note that there are some explanatory texts on larger screens.

plurals
  1. POJava 2D Game KeyListener Not Fulfilling It's Destiny
    primarykey
    data
    text
    <p>I've decided it's time to take my programming to the next step: Making A Game. So far I've been able to code everything alright, maybe having to Google some stuff I'm not too familiar with, but I feel like it <strong>should</strong> work. The graphics are being painted and all that's nice and dandy, but when it comes to the Key Listeners, that's where things went wrong. I made an inner class that implemented Key Listener, then added it in the Board constructor. I guess you'll just have to look at the code but it's pretty long and a bit messy. This is my first time doing this, so some stuff is probably unnecessary and I may just be forgetting something really obvious.</p> <p>Main Class: </p> <pre><code>import javax.swing.*; public class Game extends JFrame{ public static void main(String[] args){ new Game(); } public Game(){ add(new Board()); setTitle("Hi mom"); setSize(555,330); setResizable(false); setLocationRelativeTo(null); setDefaultCloseOperation(3); setVisible(true); } } </code></pre> <p>Board Class:</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Board extends JPanel implements ActionListener { Image background; Player p; int k; boolean moving; public Board() { setFocusable(true); Timer timer = new Timer(25, this); timer.start(); ImageIcon img = new ImageIcon(getClass().getResource("images/map.png")); background = img.getImage(); addKeyListener(new Kl()); p = new Player(); } public void paint(Graphics g) { g.drawImage(background, 0, 0, null); g.drawImage(p.getPlayer(), p.setX(30), p.setY(187), null); } public void actionPerformed(ActionEvent e) { if (k == 'W' &amp;&amp; moving == true) { p.move(0,-5); } if (k == 'S' &amp;&amp; moving == true) { p.move(0,5); } if (k == 'D' &amp;&amp; moving == true) { p.move(5,0); } if (k == 'A' &amp;&amp; moving == true) { p.move(-5,0); } repaint(); } public class Kl implements KeyListener { public void keyPressed(KeyEvent e) { k = e.getKeyCode(); moving = true; } public void keyReleased(KeyEvent e) { moving = false; } public void keyTyped(KeyEvent e) { } } } </code></pre> <p>Player Class:</p> <pre><code>import javax.swing.*; import java.awt.*; public class Player{ int x, y; Image player; public Player(){ ImageIcon img = new ImageIcon(getClass().getResource("images/player.png")); player = img.getImage(); } public Image getPlayer(){ return player; } public void move(int x, int y){ this.x += x; } public int setX(int x){ this.x = x; return x; } public int setY(int y){ this.y = y; return y; } } </code></pre>
    singulars
    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.
 

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