Jump to content
  • Advertisement
Sign in to follow this  
negflar

Problem moving sprites in JAVA

This topic is 4459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, i have a problem with my game written in java. I have implemented a KeyListener for movements of a ball with keyboard arrow key. When i press right (for example) arrow key and i hold it pressed, the ball moves on the right, but at the beginning it moves with a repeat deley just like in dos shell when i press a char-key and i hold it down (a....aaaaaaaaaa) How i can resolve this annoying problem in java? (With JNI? but i don't know how) Tank's P.S. Sorry for my english ;)

Share this post


Link to post
Share on other sites
Advertisement
Hi,

I assume you mean if you hold the key down it doesn't move at a constant rate from the start? i.e. It moves, there is a slight delay then it begins moving constantly ?

If this is the case and it is always doing it there probably isn't a way to solve it using Java's built in methods. It's probably something to do with the event dispatching thread, done on purpose to stop to many events being sent if the key wasn't being held down and merely pushed (you may just be pressing and releasing the key, but the program might have polled the keyboard many times while it was down).

All in all, I don't know how to solve this :P Try looking on Sun's site for event dispatching thread?


- Chris

Share this post


Link to post
Share on other sites
Why not have a boolean value somewhere indicating whether the desired key is down. Every keydown event set it to true, and every keyupevent set it false.

Share this post


Link to post
Share on other sites
usally i have a seprate thread for the game loop and just conect the listeners to some variables (this is for mouse but you get the idead)



int mx,my;
public boolean m1,m2;
public void mousePressed(MouseEvent e) {
if (e.getButton()==MouseEvent.BUTTON1)
m1=true;
if (e.getButton()==MouseEvent.BUTTON3)
m2=true;
mx=e.getX();
my=e.getY();
}
public void mouseReleased(MouseEvent e) {
if (e.getButton()==MouseEvent.BUTTON1)
m1=false;
if (e.getButton()==MouseEvent.BUTTON3)
m2=false;
mx=e.getX();
my=e.getY();
}

class GameLoop extends Thread{


public void run(){

if (m1){
//do something when mouse button 1 is down
}

repaint();
}



}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!