Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Java KeyListeners | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Squash Monster

New Age Retro Hippie
Togateiru Fohku Kohgeki!!
GRUNGE no HAMSTER otona bite
Peace love and turnpike!

Level: 40

Posts: 491/677
EXP: 430507
For next: 10802

Since: 03-15-04
From: Maryland (of the Country Between Canada and Mexico)

Since last post: 5 hours
Last activity: 5 hours
Posted on 12-24-04 06:04 AM Link | Quote
I managed to get the graphics to redraw in my game (switched to JPanel instead of Applet).
Here's a slightly out-dated screenshot (drag to adress bar or copy/paste)
http://squashmonster.tripod.com/Treasure/treasureGameSS2.png

Now I'm trying to read keyboard input to controll the character. I can't figure out what I'm doing wrong though.

Some excerpts from my code:
Stepper.java
class Stepper extends JPanel implements ActionListener
{
KeyboardIn keys = new KeyboardIn(this);

public Stepper()
{
addKeyListener(keys);
}

...



KeyboardIn.java

class KeyboardIn extends KeyAdapter
{ //Input from keyboard class for Treasure Game

Stepper sendTo; //Where to send events after processed

public KeyboardIn(Stepper listener)
{ //Take the Stepper we'll want to send things to.
sendTo = listener;
}

public void keyPressed(KeyEvent e)
{ //Process pressed keys
System.out.println("!");
switch (e.getKeyCode())
{ //Each key we care about has an associated binary flag. Add to it.
case KeyEvent.VK_LEFT: sendTo.takeKey(1); break;
case KeyEvent.VK_UP: sendTo.takeKey(2); break;
case KeyEvent.VK_RIGHT: sendTo.takeKey(4); break;
case KeyEvent.VK_DOWN: sendTo.takeKey(8); break;
case KeyEvent.VK_SPACE: sendTo.takeKey(16); break;
}
}

public void keyReleased(KeyEvent e)
{ //Process released keys
System.out.println("!");
switch (e.getKeyCode())
{ //Each key we care about has an associated binary flag. Subtract.
case KeyEvent.VK_LEFT: sendTo.takeKey(-1); break;
case KeyEvent.VK_UP: sendTo.takeKey(-2); break;
case KeyEvent.VK_RIGHT: sendTo.takeKey(-4); break;
case KeyEvent.VK_DOWN: sendTo.takeKey(-8); break;
case KeyEvent.VK_SPACE: sendTo.takeKey(-16); break;
}
}
}



You'll notice that my KeyboardIn class is set to print the System screen whenever anything happens. It never does though. How did I mess up? Am I using the wrong class?
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 1928/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 12-24-04 08:39 AM Link | Quote
is your JPanel surrounded by any derivative Frame class? if so, the JPanel will never see any keys...
HellSavior

Red Goomba
Level: 12

Posts: 14/41
EXP: 6398
For next: 1523

Since: 03-18-04

Since last post: 212 days
Last activity: 110 days
Posted on 12-29-04 04:08 PM Link | Quote
It would be more efficient to do something like this :

class Stepper extends JPanel implements KeyListener
{
...
public Stepper
{ setFocusable(true);
requestFocusInWindow();
addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{ switch(e.getKeyCode())
{ case KeyEvent.VK_LEFT: sendTo.takeKey(1); break;
...
}
}
}

There are a few more methods you have to implement from KeyListener, but I cannot remember them off the top of my head.

EDIT : It does not seem that this board likes spaces. Messy code it is..


(edited by HellSavior on 12-29-04 07:09 AM)
Squash Monster

New Age Retro Hippie
Togateiru Fohku Kohgeki!!
GRUNGE no HAMSTER otona bite
Peace love and turnpike!

Level: 40

Posts: 496/677
EXP: 430507
For next: 10802

Since: 03-15-04
From: Maryland (of the Country Between Canada and Mexico)

Since last post: 5 hours
Last activity: 5 hours
Posted on 01-05-05 01:53 AM Link | Quote
That's an interesting solution, though I've already got a working one now. I'll remember your version though, it should be usefull for some other projects. Thanks.
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Java KeyListeners | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.005 seconds.