2

How can I call the method keyPressed(KeyListener evt) in a loop? I want to programm a simple 2D java game, and here is my code of the keyPressed method:

private void KeyPressed(java.awt.event.KeyEvent evt) {

    key = evt.getKeyCode();

        if (key == KeyEvent.VK_W) {

            direction = 1;

            PlayerMovement();
        }
        if (key == KeyEvent.VK_S) {

            direction = 2;

            PlayerMovement();
        }
        if (key == KeyEvent.VK_D) {

            direction = 3;
        }
}
Lukas
  • 812
  • 1
  • 14
  • 43
  • 1
    Show your current code - what have you tried? What are you trying to accomplish? – nhouser9 Jul 12 '16 at 20:41
  • Ok you showed some of your code. Now tell us what you are actually trying to do. What does your program do now, and what do you want it to be doing? – nhouser9 Jul 12 '16 at 20:49
  • I want to programm a 2D java game (Graphics like pokemon), in this game I can walk bur I have to press the key every time to get an Animtion of walking. I want to accieve that I can hold the key, but it doesn't work, because if you hold a key for example 2 seconds the player walks about 4 seconds in the same direction and you can't do anything between – Lukas Jul 12 '16 at 20:54

2 Answers2

1
public class GameLoop implements Runnable {
    public void run() {
        //initialization goes here
        while(running){
            processInput();
            update();
            render();
        }
    } 
    processUpdate() {
         //here you listen to inputs
    }
}

You can set a couple booleans to track the keys are currently pressed down. For example set movingRight = true; if D is pressed and set movingRight = false; if D is released. In update you update your positions according to keys are currently pressed.

However a game loop should cointain a lot more. This is just a very basic model.

About game loops: http://gameprogrammingpatterns.com/game-loop.html

eldo
  • 1,327
  • 2
  • 16
  • 27
0

i told you have to use THREADS because here you are using the program but if you start a thread you can do what ever you want the program still works fine learn about thread because game are all about threads

private class Walk_thread extends java.lang.Thread{

public void run(){
//call the walk methode here
PlayerMovement();
}
}

so when the key is pressed create a thread that call playermovement

if (key == KeyEvent.VK_W) {

        direction = 1;
new Walk_thread().start();//here the method run is called 
System.out.println("program will not stop the thread is walking and the program is continued too");
    }
Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • thank you i know my answer was complicated but read thread tutorials because in game you will do a lot of thing in the same time – Charif DZ Jul 13 '16 at 11:00
  • implemet runnable instead of extend thread. http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread – eldo Jul 13 '16 at 11:03
  • You create a thread and handling input not creating thread on input. – eldo Jul 13 '16 at 11:05
  • Runnable interface! If you do not want to override anything of the inherited functionalities you implement interfaces not extending classes. – eldo Jul 13 '16 at 11:12
  • ok the problem is that i'm traying to explane to him the threads i'm not giving him a good answer because he needs to learn not copy past without understanding – Charif DZ Jul 13 '16 at 11:12
  • i'm telling you i'm not giving him a answer i'm explaining he will learn how to use threads any way post a answer to it question and tell him what to do i'm not in my office to write a big code for him – Charif DZ Jul 13 '16 at 11:15
  • Tchigy I am learing java for about half an year now and what we have learnd so far about Threads was 1 command : Thread.sleep(); I am the only student in my class who develops simple 2D java games and I want to learn as much as I can. – Lukas Jul 13 '16 at 14:57
  • i'm with java for 2 years now i'm steel learning threads are greate if you want to learn anything you can use vidéos tutorial you will understand then go to the PDFs and code you will understand them better this how i learned java and try to go to java EE you will find a job easly ^^ – Charif DZ Jul 13 '16 at 18:08