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 (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: KeyListener on: 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.
Pages: [1]
 

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 (8 views)
2014-07-29 18:09:19

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

Dwinin (8 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 (26 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!