Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Having Problems With The Key Listener  (Read 419 times)
0 Members and 1 Guest are viewing this topic.
Offline hobbles

Junior Member


Medals: 2
Exp: 4 years


hmm what


« Posted 2013-09-20 01:29:46 »

Man I spent hours scratching my head on this one. I have never had a problem with input before but I am kinda changing things up a little. Man I can't wait to stop letting myself get these irritating errors.

This is where I added it to the canvas.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public Main()
   {
      initGame();
     
      JFrame frame = new JFrame(gameMain.getName());
      frame.setIconImage(gameMain.setLogo());
      Canvas canvas = new Canvas();
      canvas.setPreferredSize(gameMain.getScreenSize());
     
      InputListener input = new InputListener();
      canvas.addKeyListener(input);
      canvas.addMouseListener(input);
     
      frame.add(canvas);
      frame.pack();
      frame.setVisible(true);
      canvas.setVisible(true);
      canvas.requestFocus();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
      canvas.createBufferStrategy(2);
      buffer = canvas.getBufferStrategy();
   }


This is the simple code to move in my player class and I did import the inputlistener.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   
public void input()
   {
      InputListener input = new InputListener();

      if(input.getKey() == "w")
         dir(0,-1);  
      if(input.getKey() == "a")
         dir(0,1);
      if(input.getKey() == "s")
         dir(-1,0);
      if(input.getKey() == "d")
         dir(1,0);
   }


Here is the InputListener class.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
package com.base.engine;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class InputListener implements KeyListener, MouseListener, MouseMotionListener
{
   Dimension mousePos;
   boolean clicked = false;
   boolean keyDown;
   boolean keyUp;
   String key;
   String tempKey;
   

   public Dimension mouseLoc()
   {
      return mousePos;
   }

   public void mouseMoved(MouseEvent e)
   {
      mousePos = new Dimension(e.getX(), e.getY());
   }

   public void mousePressed(MouseEvent e)
   {
      clicked = true;
   }

   public void mouseReleased(MouseEvent e)
   {
      clicked = false;
   }

   public void keyPressed(KeyEvent e)
   {
      keys(e.getKeyCode());
      keyDown = true;
   }

   public void keyReleased(KeyEvent e)
   {
      keyDown = false;
   }

   public void keyTyped(KeyEvent e)
   {

   }
   
   public void keys(int i)
   {
      if(i == KeyEvent.VK_A)
         key = "a";
      if(i == KeyEvent.VK_B)
         key = "b";
      if(i == KeyEvent.VK_C)
         key = "c";
      if(i == KeyEvent.VK_D)
         key = "d";
      if(i == KeyEvent.VK_E)
         key = "e";
      if(i == KeyEvent.VK_F)
         key = "f";
      if(i == KeyEvent.VK_G)
         key = "g";
      if(i == KeyEvent.VK_H)
         key = "h";
      if(i == KeyEvent.VK_I)
         key = "i";
      if(i == KeyEvent.VK_J)
         key = "j";
      if(i == KeyEvent.VK_K)
         key = "k";
      if(i == KeyEvent.VK_L)
         key = "l";
      if(i == KeyEvent.VK_M)
         key = "m";
      if(i == KeyEvent.VK_N)
         key = "n";
      if(i == KeyEvent.VK_O)
         key = "o";
      if(i == KeyEvent.VK_P)
         key = "p";
      if(i == KeyEvent.VK_Q)
         key = "q";
      if(i == KeyEvent.VK_R)
         key = "r";
      if(i == KeyEvent.VK_S)
         key = "s";
      if(i == KeyEvent.VK_T)
         key = "t";
      if(i == KeyEvent.VK_U)
         key = "u";
      if(i == KeyEvent.VK_V)
         key = "v";
      if(i == KeyEvent.VK_W)
         key = "w";
      if(i == KeyEvent.VK_X)
         key = "x";
      if(i == KeyEvent.VK_Y)
         key = "y";
      if(i == KeyEvent.VK_Z)
         key = "z";
   }
   
   
   public String getKey()
   {
     
      return key;
   }
   
   public boolean getKeyPress()
   {
      return keyDown;
   }

   @Override
   public void mouseDragged(MouseEvent e) {}
   @Override
   public void mouseClicked(MouseEvent e) {}
   @Override
   public void mouseEntered(MouseEvent e) {}
   @Override
   public void mouseExited(MouseEvent e) {}
}

I'm upset I didn't capitalize the H in my name...
Offline Troncoso

JGO Coder


Medals: 20



« Reply #1 - Posted 2013-09-20 02:47:45 »

So....you gonna talk about these errors a bit? Or are we just admiring your code?
Offline hobbles

Junior Member


Medals: 2
Exp: 4 years


hmm what


« Reply #2 - Posted 2013-09-20 03:15:34 »

I was letting it bake in a little Wink. I'm just trying to move the the character and I have done it several different ways. Even had the keylistener in the player class with the code in the keypressed method and it still wasn't working. I'm just lost I don't really see much difference between the other games I was doing.

I'm upset I didn't capitalize the H in my name...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Troncoso

JGO Coder


Medals: 20



« Reply #3 - Posted 2013-09-20 03:23:40 »

Well, the InputListener that you initialize in the input() method is not the same InputListener that you added to your canvas. So, that's one issue. Nothing is actually listening to the key presses. You should only have one InputListener that you pass around.

EDIT: Actually, you should just poll the keys in your main class, and pass the results to your player.
Offline hobbles

Junior Member


Medals: 2
Exp: 4 years


hmm what


« Reply #4 - Posted 2013-09-20 03:36:20 »

Sweet thanks a lot seems like something I should have noticed...

I'm upset I didn't capitalize the H in my name...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (11 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (25 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!