Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
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  
  KeyListener  (Read 1741 times)
0 Members and 1 Guest are viewing this topic.
Offline Gewran

Junior Newbie




Java games rock!


« Posted 2004-09-06 13:03:57 »

Hello everyone

I'm currently learning java at school, so I get these program examples that I should copy and compile, to see how the code works. The problem is, that the one I'm currently working on doesn't work, and I havent been able to figure out why. The program is an applet game that lets the user control a little blue dot, and move it around the screen using the arrow keys. The problem is that once I run the applet, the dot won't move. Could anyone help me with this?

Source code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class KeyApp4 extends Applet implements Runnable, KeyListener
{
     // Bilens variabler
     int RADIE = 20;
     double bilx = 200, bily = 200;
     double vx = 0, vy = 0;
     
     // Fönsterstorleksvariabler
     Dimension appletSize;
     
     // Knapptryckningsvariabler
     boolean left = false, right = false, up = false, down = false;
     
     public void init()
     {
           appletSize = getSize();
           addKeyListener( this);
           requestFocus();
     }
     
     public void paint( Graphics g)
     {
           // Ritar bilen
           g.setColor( Color.blue);
           g.fillOval( (int) (bilx - RADIE), (int) (bily - RADIE), 2*RADIE, 2*RADIE);
     }
     
     public void start()
     {
           // Ny animeringstråd
           Thread tråd = new Thread( this);
           tråd.start();
     }
     
     public void run()
     {
           while( true) {
                 // Ändrar hastigheten beroende på användarens knapptryckningar
                 if( left)      vx = vx - 0.05;
                 if( right)      vx = vx + 0.05;
                 
                 // Kollar så att bilen stannar när den träffar väggen
                 if( bilx + RADIE > appletSize.width || bilx - RADIE < 0)       vx = 0;
                 if( bily + RADIE > appletSize.height || bily - RADIE < 0)       vy = 0;
                 
                 // Förflyttar bilen
                 bilx = bilx + vx;
                 bily = bily + vy;
                 
                 repaint();
                 
                 try { Thread.sleep( 10); } catch( InterruptedException ie) {}
           }
     }
     
     public void keyTyped( KeyEvent e) {}

     public void keyPressed(KeyEvent e) {
           int key = e.getKeyChar();
           if (key == KeyEvent.VK_LEFT) left = true;
           if (key == KeyEvent.VK_RIGHT) right = true;
           if (key == KeyEvent.VK_UP) up = true;
           if (key == KeyEvent.VK_DOWN) down = true;
     }
     
     public void keyReleased(KeyEvent e) {
           int key = e.getKeyChar();
           if (key == KeyEvent.VK_LEFT) left = false;
           if (key == KeyEvent.VK_RIGHT) right = false;
           if (key == KeyEvent.VK_UP) up = false;
           if (key == KeyEvent.VK_DOWN) down = false;
     }
}
Offline woogley
« Reply #1 - Posted 2004-09-06 14:27:38 »

Quote

double vx = 0, vy = 0;


there's your problem. those v's stand for "velocity" I imagine, change those variables to something other than 0 and it should work, because:
1  
2  
3  
// Förflyttar bilen 
  bilx = bilx + vx;
   bily = bily + vy;

won't work unless vx and/or vy is not 0
Offline Gewran

Junior Newbie




Java games rock!


« Reply #2 - Posted 2004-09-07 09:16:07 »

Thanks alot for the help, but unfortunately i did not help me.

"double vx = 0, vy = 0;
there's your problem. those v's stand for "velocity" I imagine, change those variables to something other than 0 and it should work, because:
Code:
// Förflyttar bilen  
  bilx = bilx + vx;  
  bily = bily + vy;  

won't work unless vx and/or vy is not 0"

Yes, v stands for veliocity, and vx for the veliocity in the x-axis. vy stands for the veliocity in the y-axis. But I may have to clarify a little:

public void keyPressed(KeyEvent e) {  
 int key = e.getKeyChar();  
 if (key == KeyEvent.VK_LEFT) left = true;  
 if (key == KeyEvent.VK_RIGHT) right = true;  
 if (key == KeyEvent.VK_UP) up = true;  
 if (key == KeyEvent.VK_DOWN) down = true;  
}
 
public void keyReleased(KeyEvent e) {  
 int key = e.getKeyChar();  
 if (key == KeyEvent.VK_LEFT) left = false;  
 if (key == KeyEvent.VK_RIGHT) right = false;  
 if (key == KeyEvent.VK_UP) up = false;  
 if (key == KeyEvent.VK_DOWN) down = false;  
}

The thing is that I have defined keyReleased and keyPressed myself, to make them set boolean values to true/false depending on if any of the arrow-keys is pressed or released.

public void run()
{
 while( true) {
  // Ändrar hastigheten beroende på användarens knapptryckningar
  if( left) vx = vx - 0.05;
  if( right) vx = vx + 0.05;
   
  // Kollar så att bilen stannar när den träffar väggen
  if( bilx + RADIE > appletSize.width || bilx - RADIE < 0)  vx = 0;
  if( bily + RADIE > appletSize.height || bily - RADIE < 0)  vy = 0;
   
  // Förflyttar bilen
  bilx = bilx + vx;
  bily = bily + vy;
   
  repaint();
   
  try { Thread.sleep( 10); } catch( InterruptedException ie) {}
 }
}

Then these boolean values would be used to make the dot's veliocity to increase:

if( left) vx = vx - 0.05;
if( right) vx = vx + 0.05;

(Note that I haven't written the code for the up and down keys yet)

Finally the dot is moved and the picture repainted:

bilx = bilx + vx;
bily = bily + vy;
   
repaint();

My problem is that the dot wont move event though I press the arrow-keys like a maniac. Any help is appreciated.

\\Gewran  Huh
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #3 - Posted 2004-09-07 14:32:03 »

You chck for collisions against walls, and then set the velocity on that axis to 0.  That means that the ball will STAY in collision with the wall.  When you detect a collision, you should either repel the velocity for a bounce effect, or you should make the ball stop (as you're doing) and move the ball back within the play area so that it's no longer colliding with the wall.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #4 - Posted 2004-09-07 18:21:43 »

Don't forget to click in the Applet portion of the web page to give the Applet the focus.  When the web page loads the Applet, it maintains control of the input focus.  You have to click on the Applet to give it the input focus.

Offline Gewran

Junior Newbie




Java games rock!


« Reply #5 - Posted 2004-09-08 08:43:22 »

Quote
You chck for collisions against walls, and then set the velocity on that axis to 0.  That means that the ball will STAY in collision with the wall.  When you detect a collision, you should either repel the velocity for a bounce effect, or you should make the ball stop (as you're doing) and move the ball back within the play area so that it's no longer colliding with the wall.


That bouncing thing seems good. I'll try to implement it as soon as I've got this problem sorted out.

Quote
Don't forget to click in the Applet portion of the web page to give the Applet the focus.  When the web page loads the Applet, it maintains control of the input focus.  You have to click on the Applet to give it the input focus.


I've tried this and it doesn't work. My applet does ask for focus in the init() method:

public void init()
{
 appletSize = getSize();
 addKeyListener( this);
 requestFocus();
}

And I belive this is the problem. That my applet doesn't get the keyboard focus even though it should. Is there any other way to get the keyboard focus?

Thanks for the help.

\\Gewran
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #6 - Posted 2004-09-08 10:29:18 »

Remember that it is requestFocus - i.e. not guaranteed to be obeyed. This is one of those really annoying thigns, because it's rather hard to see how you can cope if your *request* is not fulfilled Sad.

malloc will be first against the wall when the revolution comes...
Offline Gewran

Junior Newbie




Java games rock!


« Reply #7 - Posted 2004-09-08 11:44:55 »

Quote
Remember that it is requestFocus - i.e. not guaranteed to be obeyed. This is one of those really annoying thigns, because it's rather hard to see how you can cope if your *request* is not fulfilled .


Is there any way to make sure you get the focus, or am I stuck with no way to make it work?
Offline Borgil

Innocent Bystander




Java games rock!


« Reply #8 - Posted 2004-09-11 08:03:44 »

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class KeyApp4 extends Applet implements Runnable, KeyListener
{
// Bilens variabler
int RADIE = 20;
double bilx = 200, bily = 200;
double vx = 0, vy = 0;

// Fönsterstorleksvariabler
Dimension appletSize;

// Knapptryckningsvariabler
boolean left = false, right = false, up = false, down = false;

public void init()
{
 appletSize = getSize();
 addKeyListener( this);
 requestFocus();
}

public void paint( Graphics g)
{
 // Ritar bilen
 //System.out.println("paint()");
 g.setColor( Color.blue);
 g.fillOval( (int) (bilx - RADIE), (int) (bily - RADIE), 2*RADIE, 2*RADIE);
}

public void start()
{
 // Ny animeringstråd
 Thread tråd = new Thread( this);
 tråd.start();
}

public void run()
{
 while( true) {
  // Ändrar hastigheten beroende på användarens knapptryckningar
  if( left) vx = vx - 0.05;
  if( right) vx = vx + 0.05;
  if( up) vy = vy + 0.05;
  if( down) vy = vy - 0.05;

  // Kollar så att bilen stannar när den träffar väggen

  /*
   * This is just modified safegaurds.
   *
   * Just setting the velocity to 0 will result in the ball getting stuck
   * because part of it is already outside the bounds of the applet and the
   * velocity will continually be set to 0. Need to set velocity to 0 then
   * reposition the ball so it is completely inside the bounds of the applet.
   */
  if( bilx + RADIE > appletSize.width)
  {
        vx = 0;
        bilx = appletSize.width-RADIE;
     }
     if( bilx - RADIE < 0)
     {
           vx = 0;
           bilx = RADIE;
     }
  if( bily + RADIE > appletSize.height)
  {
        vy = 0;
        bily = appletSize.height-RADIE;
        }
     if( bily - RADIE < 0)
     {
           vy = 0;
           bily = RADIE;
     }
     //end modified safegaurds.


  // Förflyttar bilen
  bilx = bilx + vx;
  bily = bily + vy;

  repaint();

  try { Thread.sleep( 10); } catch( InterruptedException ie) {}
 }
}

public void keyTyped( KeyEvent e) {}


/*
* The values 50, 52, 54, and 56 are just the values
* that correspond to my non-standard keyboard. This was
* where your problem was. Just changing these values made
* the applet work for me.
*/
public void keyPressed(KeyEvent e) {
 int key = e.getKeyChar();
 if (key == 52) left = true;
 if (key == 54) right = true;
 if (key == 50) up = true;
 if (key == 56) down = true;
}

public void keyReleased(KeyEvent e) {
 int key = e.getKeyChar();
 if (key == 52) left = false;
 if (key == 54) right = false;
 if (key == 50) up = false;
 if (key == 56) down = false;
}
}

This code works great for me. All I did was change values in keyPressed and keyReleased that key was being checked against. You may need to change them since I use a non-standard keyboard, but I think this is where your problem was. Did you use Systm.out.println(key); to test if the keyboard input was matching VK_LEFT ect... ?

Note that when running the applet I did have to click in the applet's bounds before keyboard input would register.

I hope this helps.
Offline barfy

Junior Member




The evidence of things not seen


« Reply #9 - Posted 2004-09-11 09:13:01 »

Ha! That should work:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
    public void keyTyped( KeyEvent e) {}

      public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode(); // get the key CODE

            if (key == KeyEvent.VK_LEFT) left = true;
            if (key == KeyEvent.VK_RIGHT) right = true;
            if (key == KeyEvent.VK_UP) up = true;
            if (key == KeyEvent.VK_DOWN) down = true;
      }
      
      public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode(); // get the Key CODE

            if (key == KeyEvent.VK_LEFT) left = false;
            if (key == KeyEvent.VK_RIGHT) right = false;
            if (key == KeyEvent.VK_UP) up = false;
            if (key == KeyEvent.VK_DOWN) down = false;
      }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gewran

Junior Newbie




Java games rock!


« Reply #10 - Posted 2004-09-11 12:25:15 »

Yes that worked! Thanks alot!

\\Gewran
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.

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

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

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

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

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
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!