Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  keyTyped not working?  (Read 1505 times)
0 Members and 1 Guest are viewing this topic.
Offline GustavXIII

Junior Devvie


Projects: 1



« Posted 2011-03-24 12:01:53 »

Hi there!
So I working on the movement in my RPG.
Now it is so:
The Hero goes AND runs 16 Pixel(keyPressed).
But now I want that the Hero GOES 32 Pixel. And run 16 Pixel.

So I thought Im using keyTyped, when the Player push LEFT he move 32 Pixel.If he hold it pressed he mobe 16.
BUT it dont work Sad

Did I do something wrong in keyTyped?

[code=java]
public void keyTyped(int key, char c)
    {
    if(schalter.MovementAllowed){

            switch(key)
            {
                case Input.KEY_UP:
                        if(used==false){
         TypedUP=true;
                        used=true;
                        lastDirection=3;
                        movement=true;
                     }
                     break;
          }
  }
}

[/code]

At my Movement Code
[code=java]
 if(TypedRIGHT && schalter.player_x<Kollision.getWidth()-40
          && blocked[schalter.player_x+48][schalter.player_y+48] == false
          && blocked[schalter.player_x+48][schalter.player_y+61] == false
          && schalter.player_x +32 != npcKollision_X[0]
          && schalter.MovementAllowed)
      {
         p.x=p.x-32;
         schalter.player_x=schalter.player_x+32;
      }
[/code]

EDIT: Ok I think I know how to make it without. Im using this...Or not -.-"

Offline Captain Awesome

Junior Devvie


Medals: 2


Hi


« Reply #1 - Posted 2011-03-24 15:29:24 »

keyTyped only runs when you press AND release the key. Unless you really want that behaviour, I suggest you override the keyPressed method instead.

EDIT: Wait a second, I guess you aren't using a keylistener since the keyTyped method doesn't have a single KeyEvent argument. Where did you get that code from?
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-03-24 18:00:37 »

Umm...how are you getting input?
What defines "keyTyped(int key, char c)"? What is "Input"?

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

Junior Devvie


Projects: 1



« Reply #3 - Posted 2011-03-24 21:22:48 »

Im using Slick. In

public void init(GameContainer gc)throws SlickException
    {

I do
1  
2  
3  
4  
5  
6  
7  
if (gc instanceof AppGameContainer) {
         app = (AppGameContainer) gc;
      }

     
         i = gc.getInput();   //INPUT
         


Ok so keyTyped would not work how I wanted.

Is there another method so that if the player press ONES he moves 32 Pixel? Else 16.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-03-25 02:28:26 »

Oh I've never used Slick so I can't help much Grin

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 81
Projects: 15


★★★★★


« Reply #5 - Posted 2011-03-25 10:13:41 »

One way you can handle this problem is using the delta time (provided in the update method) to calculate how long a key is pressed.

so something like

if (input.isKeyDown(Input.KEY_LEFT)) {
    leftButtonDownTime += delta;
}

Then depending on how long its held down you move 16px or 32px.

Hope that helps.
Offline ReBirth
« Reply #6 - Posted 2011-03-25 10:40:39 »

Check out if your program received your press first (by put in System.out.println). If yes, something wrong with your move method.

Offline GustavXIII

Junior Devvie


Projects: 1



« Reply #7 - Posted 2011-03-25 10:49:09 »

One way you can handle this problem is using the delta time (provided in the update method) to calculate how long a key is pressed.

so something like

if (input.isKeyDown(Input.KEY_LEFT)) {
    leftButtonDownTime += delta;
}

Then depending on how long its held down you move 16px or 32px.

Hope that helps.

EDIT: Ok I tested it some time and it doesnt is really negative so I let it this way. Thanks Kappa.

Yeah good idea!!
It work with just one beautyerror.

So if I press once he move 32px.
BUT if I hold pressed he moves ONE time 32px and after that 16px.
Yes I know why, because of my If(). But I dont know how to change it.
But I think I could live with it. First xD
I made it this way:

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  
public void update(GameContainer gc, int delta) throws SlickException
    {

     START = System.currentTimeMillis();

     checkBewegung();
     checkMessageBox();
     checkMapTeleport();
     checkMechanik(gc);
     checkNPC(); //NPCs und Truhen prüfen

     delta=10;


     if (i.isKeyDown(Input.KEY_LEFT))
       leftButtonDownTime += delta;
     

 
     AUSFUEHR = System.currentTimeMillis()-START;

     if(60 > (int)AUSFUEHR)
             gc.sleep(60-(int)AUSFUEHR);


    }


Movement Method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
 else if(LEFT && schalter.player_x>0
             && blocked[schalter.player_x][schalter.player_y+48] ==false
             && blocked[schalter.player_x][schalter.player_y+61] ==false
             && schalter.BewegungErlaubt )
     {

            if(leftButtonDownTime<10){
              p.x=p.x+32;
             schalter.player_x=schalter.player_x-32;
            }

        else {
           p.x=p.x+16;
          schalter.player_x=schalter.player_x-16;
         }

      }


In the keyPressed I just did
1  
2  
3  
4  
5  
6  
7  
case Input.KEY_LEFT:
                      LEFT=false;
                      TypedLEFT=false;
                      benutzt=false;
                      bewegung=false;
                      leftButtonDownTime=0;
                      break;

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.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (59 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!