Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (306)
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  
  [SOLVED] Fast and slow KeyAdapter(KeyInputHandler)  (Read 1352 times)
0 Members and 1 Guest are viewing this topic.
Offline zetsin

Senior Newbie





« Posted 2012-11-03 18:14:21 »

Hello everybody! I wanna make little 2D quest-game, i added a Hero and KeyAdapter... But!
On my Linux-notebook Hero walking not slow and not fast(like in all games).
On my Windows-PC Hero walking very fast. 1 keyPress on PC = 10 keyPress on notebook. How can i fix it? Thanks for help.

Code of KeyInputHandler:
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  
package code;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInputHandler extends KeyAdapter {

    private byte direction = 0;
    private boolean jumped = false;
    /*
     * 0 - no
     * 1 - right
     * 2 - left
     */


    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            direction = 2;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            direction = 1;
        }
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            jumped = true;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            direction = 0;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            direction = 0;
        }
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            jumped = false;
        }
    }
   
    public byte getDirection(){
        return direction;
    }
   
    public boolean getJumped(){
        return jumped;
    }
}


Part of code in Main 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  
@Override
    public void run() {
        long lastTime = System.currentTimeMillis();
        long delta;
        try {
            init();
        } catch (IOException ex) {
            Logger.getLogger(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        while (running) {
            delta = System.currentTimeMillis() - lastTime;
            lastTime = System.currentTimeMillis();
            update(delta);
            render();
        }
    }
   
    private void update(long delta) {
        if (KIH.getDirection() == 2) {
            level.walkBackward();
        }
        if (KIH.getDirection() == 1) {
            level.walkForward();
        }
        if (KIH.getJumped()){
            level.heroAction();
        }
    }
Offline Agro
« Reply #1 - Posted 2012-11-03 19:44:19 »

Well, you're not implementing any constant frame rate method. You're almost there, but you're not telling the game to sleep any X amount of time.

Here's some code I use:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
@Override
   public void run() {
      long beforeTime, timeDiff, sleep;

        beforeTime = System.currentTimeMillis();
     
        while(true) {
           update();
           repaint();
           
           timeDiff = System.currentTimeMillis() - beforeTime;
            sleep = FRAME_DELAY - timeDiff;
            if (sleep < 0)
                sleep = 2;
            try {
                Thread.sleep(sleep);
            } catch (InterruptedException e) {
               
            }

            beforeTime = System.currentTimeMillis();
        }
   }


It keeps a fairly constant frame rate for me.

Offline zetsin

Senior Newbie





« Reply #2 - Posted 2012-11-03 20:40:07 »

Thanks a lot! It works! )
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
mrbenebob (4 views)
2013-06-19 14:55:23

BrassApparatus (12 views)
2013-06-19 08:52:37

NegativeZero (17 views)
2013-06-19 03:31:52

NegativeZero (19 views)
2013-06-19 03:24:09

Jesse_Attard (21 views)
2013-06-18 22:03:02

HeroesGraveDev (62 views)
2013-06-15 23:35:23

Vermeer (61 views)
2013-06-14 20:08:06

davedes (61 views)
2013-06-14 16:03:55

alaslipknot (56 views)
2013-06-13 07:56:31

Roquen (77 views)
2013-06-12 04:12:32
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
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!