• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help  RSS feed

 
Tom Schuman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want an integer to raise when i press the right arrow button and stop rising when i relase it. I have tried doing it, but it goes too fast and it never stops. I need some way to have a pause in my loop or shall i even use a loop? I'm very new to java so try to explain thoroughly.
Thanks in advance.

This is the code i have now:

while (key == 1006)// left arrow
heading1++;

if (key == 1006 || key == 1007) // left or right key released
{
key = 0;
return false;
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you flesh out the program a bit ... is this in a Swing event handler?

It looks as though the variable "key" is being set somewhere ahead of this code. For the loop to ever end we'll have to have a way for key to change to some other value. Maybe you should only do this code once - no loop - and wait for another key event, or maybe you need to read another key from inside the loop.
 
Tom Schuman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a game where you can turn freely around pressing left or right and then walk to the direction you have turned. I think its best if u tell me how i should do this. I believe i can make the part where u move in the direction your facing but i can't try it if i don't know how to keep track of where the player is facing. I would be really happy if i get this done, i have been stuck so long with this problem

Can you flesh out the program a bit ... is this in a Swing event handler?


Sorry i don't know what Swing event handler is but here is a link to the game.

http://www.freewebs.com/blobber/KillersJar.html
-Keys-

--Player1--

Up: Up arrow
Down: Down arrow
Left: Left arrow
Right: Right arrow

--Player2--

Up: W
Down: S
Left: A
Right: D
 
Tom Schuman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is noone able to help me
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally in this forum, we focus on helping poeple solve their own problems, not handing them code to do what they want.

Also, everyone here is volunteering their time. People help out when they can. If nobody's helped you so far, then nobody's had time - it is the holiday season, afterall...

if the code you've written above is your actual code, you have a loop that will never end. a While-loop has ONE THING in it's body. That thing can be either a) a SINGLE line of code, or b) a pair of curly braces {} that enclose multiple lines. Since you didn't use braces, you get the one line.

What happens in your one line? you increase the integer. the value of 'key' didn't change, so your condition is still true, so you loop again...

you need, at the very least, to enclose more code in curly braces. something like


now, this still won't work exactly. why? becase you're never changing the value of key. you're testing to see if it changed (good!!!), but there is no code to actually change it. that's why Stan suggested you learn about listeners.

or, as Stan also suggests, put code here that will change the value of key. How did you get it equal to 1006 before this code? you need to do something like that again WITHIN the body of this loop.

If you post more code, and ask more focused question, you're also more likely to get a reponse than just saying "Somebody write this code and give it to me"

just a few helpful tips.
[ December 31, 2004: Message edited by: fred rosenberger ]
 
Tom Schuman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key variable is changed in keydown method but thats outside the loop. Is it possible to change a variable in a loop from outside the loop? I don't understand how i could make the loop stop when i relase left arrow.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!