The use of KeyListeners to make an object move in Applet
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, May 30, 2006 07:20 PM

How do use KeyListeners to make an object move in applets? I have the KeyListener format set up with keyPressed(KeyEvent e) etc., but after that I've no idea how to incorporate that with the run(). I want the obj to move right when the right arrow key is pressed and to move left when the left arrow key is pressed. All within the boundaries of the applet of course.

Re: The use of KeyListeners to make an object move in Applet

Posted By:   Robert_Lybarger  
Posted On:   Tuesday, May 30, 2006 08:35 PM

Been a long while since I've done applets, but you don't need the keyEvent method to necessarily have anything to do with run(). Treat the combination of the init() and run() method like a sort of main() method in a normal program. When the keyEvent is processed, decide which key was used, increment your location for the object, and then just trigger a repaint. The run() method doesn't need to be really involved in the event handling code.
About | Sitemap | Contact