Topic:   Arrow Keys   (Read 23479 times)


0 Members and 1 Guest are viewing this topic.

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Arrow Keys
« on: October 20, 2010, 06:28:38 PM »
I know how to make the keyboard work, but how do you make keys work smoothly? Example: I say "If the right key is down push this sprite 5 pixels right" and then I try it, and if you hold down the key it fires once and then after a moment fires a bunch of times like if I was typing... It wont work for an action game at all. It's strobelike, like a stop motion.
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Arrow Keys
« Reply #1 on: October 20, 2010, 06:55:25 PM »
On keydown make a Boolean for Down Arrow. If it's hit then set it to TRUE. Then on keyup if the Down Arrow is let up set the Boolean to FALSE.
Then have a movement timer that if the Down Arrow Boolean is TRUE then move the player down.

Repeat for the rest of the arrow keys and presto!


-Gan

WarHampster


  • GMG Extraordinaire

  • ***


  • Posts: 1501

  • The People's Moderator
    • Arcade of the Absurd
Re: Arrow Keys
« Reply #2 on: October 20, 2010, 07:15:52 PM »
Here's what SC3D looks like:

In card 1:

Open card:

Code: [Select]
// main loop
IF MAPLOADED = TRUE THEN
 Â  ON TIMER 1
 Â     
 Â     // if a key has been hit, check what it is.
 Â     // I had to include seperate statements for the
 Â     // option, command, control, and shift keys because of the way that SC handles input
 Â     IF keyhit = TRUE THEN
 Â        KEYDOWN
 Â     END IF
 Â     
 Â     IF OPTIONKEY = TRUE THEN
 Â        KEYDOWN
 Â     END IF
 Â     
 Â     IF COMMANDKEY = TRUE THEN
 Â        KEYDOWN
 Â     END IF
 Â     
 Â     IF SHIFTKEY = TRUE THEN
 Â        KEYDOWN
 Â     END IF
 Â     
 Â     IF CONTROLKEY = TRUE THEN
 Â        KEYDOWN
 Â     END IF
 Â     
 Â     // render the world and show the frame
 Â     LET oktorender = true
 Â     IF changed = TRUE THEN
 Â        UPDATESCREEN
 Â        LET changed = false
 Â     END IF
 Â     
 Â  END TIMER
END IF

Keydown:

Code: [Select]
LET keyhit = TRUE

Global:

KEYDOWN method:

Code: [Select]

LET blockotherkeys = FALSE

// option - move up
IF OPTIONKEY = TRUE THEN
 Â // do stuff
 Â LET blockotherkeys = TRUE // becuase SC handles key presses strangely
END IF

// command - move down
IF COMMANDKEY = TRUE THEN
 Â  // do stuff
 Â  LET blockotherkeys = TRUE
END IF

// shift - look up
IF SHIFTKEY = TRUE THEN
 Â  // do stuff
 Â  LET blockotherkeys = TRUE
END IF

// control - look down
IF CONTROLKEY = TRUE THEN
 Â  // do stuff
 Â  LET blockotherkeys = TRUE
END IF

IF blockotherkeys = FALSE THEN
 Â 
 Â  // turn left - left arrow
 Â  IF KEY$ = LEFTARROW$ THEN
 Â     // do stuff
 Â  END IF
 Â 
 Â  // turn right - right arrow
 Â  IF KEY$ = RIGHTARROW$ THEN
 Â     // do stuff
 Â  END IF
 Â 
 Â  // move forward - up arrow
 Â  IF KEY$ = UPARROW$ THEN
 Â     // do stuff
 Â  END IF
 Â 
 Â  // move backwards - down arrow
 Â  IF KEY$ = DOWNARROW$ THEN
 Â     // do stuff
 Â  END IF
 Â 
END IF

LET keyhit = false // tell the main loop to get on with it
LET changed = true // tell the engine that something is different
« Last Edit: October 20, 2010, 07:17:50 PM by WarHampster »

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: Arrow Keys
« Reply #3 on: November 27, 2010, 09:16:28 PM »
Quote
On keydown make a Boolean for Down Arrow. If it's hit then set it to TRUE. Then on keyup if the Down Arrow is let up set the Boolean to FALSE.
Then have a movement timer that if the Down Arrow Boolean is TRUE then move the player down.

Repeat for the rest of the arrow keys and presto!


-Gan
Thing is there's no key up, and maybe it's my computer but I don't see how all this helps with the whole problem of having to hold down a key and wait to make something move...
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

WarHampster


  • GMG Extraordinaire

  • ***


  • Posts: 1501

  • The People's Moderator
    • Arcade of the Absurd
Re: Arrow Keys
« Reply #4 on: November 27, 2010, 09:38:26 PM »
Yeah I realized that the code I posted doesn't actually fix the problem... I think SC3D was lagging to such an extent that I didn't notice the input lag and thought that it was fixed.

As far as I know there is currently no way to avoid this :(

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: Arrow Keys
« Reply #5 on: November 27, 2010, 09:53:39 PM »
All right, well I guess I can't really use SC for a shooter game then, can I? It's cool tho, maybe there will be a fix for it later. I think right now if I want to do action games I'll be using TNT anyways, it's a very cool program, and not a whole lot harder to learn. (hint hint)
 Â    However when I get back to that cool point-and-click game I started I'll switch back to Silver Creator, because that would probably be harder in TNT instead of easier. And now I'm getting more practice with programming in general I might take another shot at programming an inventory screen.
And thanks for the help guys! I may use this code for some other games if or when I want key shortcuts.
« Last Edit: November 27, 2010, 09:55:09 PM by Connors »
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Charlo


  • GMG-er

  • **


  • Posts: 451
Re: Arrow Keys
« Reply #6 on: November 27, 2010, 10:23:06 PM »
I think that the "delay" is determined by the system (you can change it under System Preferences).  I'm not sure if RealBasic (and by extension, SilverCreator) has the ability to change it or do anything about it.  This is just speculation,  I could be wrong.

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: Arrow Keys
« Reply #7 on: November 28, 2010, 12:03:33 AM »
Quote
I think that the "delay" is determined by the system (you can change it under System Preferences).  I'm not sure if RealBasic (and by extension, SilverCreator) has the ability to change it or do anything about it.  This is just speculation,  I could be wrong.
TNTBasic seems to have a way around it. It checks wether the key is still pressed each frame. I'd like know the cause of it, however, and what Silver Creator does differently. It's kind of interesting.
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Arrow Keys
« Reply #8 on: November 28, 2010, 07:28:43 AM »
You could incorporate a "stop" button and have keydown code execute on a timer instead. Something like this: (GM syntax)

Code: [Select]
ON TIMER 1
 Â IF keydown$ = "UPARROW" THEN
 Â   ' Code to execute while up arrow is the last button pressed.
 Â END IF
 Â IF keydown$ = "DOWNARROW" THEN
 Â   ' Code to execute while down arrow is the last button pressed.
 Â END IF
 Â IF keydown$ = "LEFTARROW" THEN
 Â   ' Code to execute while left arrow is the last button pressed.
 Â END IF
 Â IF keydown$ = "RIGHTARROW" THEN
 Â   ' Code to execute while right arrow is the last button pressed.
 Â END IF
END TIMER
Then, as soon as an unassigned button is pressed (spacebar for example), the timer routine will stop executing the code for the arrow buttons.
« Last Edit: November 28, 2010, 04:20:56 PM by Silverwind »
I survived the spammage of 2007

EqwanoX


  • Administrator

  • GMG Extraordinaire

  • *****


  • Posts: 1180
Re: Arrow Keys
« Reply #9 on: November 28, 2010, 09:42:44 AM »
Quote
I know how to make the keyboard work, but how do you make keys work smoothly? Example: I say "If the right key is down push this sprite 5 pixels right" and then I try it, and if you hold down the key it fires once and then after a moment fires a bunch of times like if I was typing... It wont work for an action game at all. It's strobelike, like a stop motion.
if you can explain what you want it to do then i can probly help. but what you explain here seems right: press arrow key once it moves once, hold arrow key it keeps moving. i dont see what the problem is

WarHampster


  • GMG Extraordinaire

  • ***


  • Posts: 1501

  • The People's Moderator
    • Arcade of the Absurd
Re: Arrow Keys
« Reply #10 on: November 28, 2010, 10:50:31 AM »
Eq, the issue is that when you hold down a key, there's a lag between when SC recognizes that a key is pressed and when it recognizes that a key is held down.

Silver, I'm pretty sure that I've tried that and it didn't work, although logically it should. I'll try to work on this a bit later.

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Arrow Keys
« Reply #11 on: November 28, 2010, 12:13:28 PM »
Oh. Here's a GM example: http://www.mediafire.com/?7c312c1v639i4gv

And here's the code:

Code: [Select]
spriteX = 286
spriteY = 132
SPRITE 1 spriteX spriteY ship.gif

ON KEYDOWN
  SETWINDOWTITLE Last key pressed: $keydown$$
END KEYDOWN

ON TIMER 1
  YMoveAmount = 0
  XMoveAmount = 0
  IF keydown$ = "UPARROW" THEN YMoveAmount = -5
  IF keydown$ = "DOWNARROW" THEN YMoveAmount = 5
  IF keydown$ = "LEFTARROW" THEN XMoveAmount = -5
  IF keydown$ = "RIGHTARROW" THEN XMoveAmount = 5
  spriteX = spriteX + XmoveAmount
  spriteY = spriteY + YmoveAmount
  SPRITE 1 spriteX spriteY
END TIMER

And here's a screencast, you lucky people: http://screencast.com/t/8DaMq2OSHX9
I survived the spammage of 2007

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Arrow Keys
« Reply #12 on: November 28, 2010, 12:59:24 PM »
I've just tried your code in Sc and it turns out that KEY$ stays equal to the last key pressed until a new key is pressed. It's unfortunate.

Quote
I did figure a way to make smooth movement, though the player keeps moving a second after the key was depressed:
Open Card
Code: [Select]
CREATESPRITE 1, "guy"
MOVESPRITE 1, 210, 100
LET moveCount = 0

ON TIMER 0
 Â 
 Â  IF moveCount > 0 THEN
 Â     IF KEY$ = UPARROW$ THEN
 Â        PUSHSPRITE 1, 0, -1
 Â     END IF
 Â     IF KEY$ = DOWNARROW$ THEN
 Â        PUSHSPRITE 1, 0, 1
 Â     END IF
 Â     IF KEY$ = LEFTARROW$ THEN
 Â        PUSHSPRITE 1,
-1, 0
 Â     END IF
 Â     IF KEY$ = RIGHTARROW$ THEN
 Â        PUSHSPRITE 1, 1, 0
 Â     END IF
 Â     LET moveCount = moveCount - 1
 Â  END IF
END TIMER

Key Down
Code: [Select]
LET moveCount = 80

I just figured a better way:
Open Card
Code: [Select]
CREATESPRITE 1, "guy"
MOVESPRITE 1, 210, 100
LET moveCount = 0
LET LASTARROW$ = ""

ON TIMER 0
  
   IF moveCount > 0 THEN
      IF KEY$ = UPARROW$ THEN
         PUSHSPRITE 1, 0, -1
      END IF
      IF KEY$ = DOWNARROW$ THEN
         PUSHSPRITE 1, 0, 1
      END IF
      IF KEY$ = LEFTARROW$ THEN
         PUSHSPRITE 1, -1, 0
      END IF
      IF KEY$ = RIGHTARROW$ THEN
         PUSHSPRITE 1, 1, 0
      END IF
      LET moveCount = moveCount - 1
   END IF
END TIMER
Key Down
Code: [Select]
IF moveCount = 0 THEN
   LET moveCount = 80
ELSE
   LET moveCount = 14
END IF
IF LASTARROW$ = KEY$ THEN
   ELSE
   LET moveCount = 80
END IF
LET LASTARROW$ = KEY$

The movement is nearly perfect. The only issue is if you tap an arrow key once it moves for a second before stopping.

Here's a video: http://cl.ly/380B1B352g1G1d1y2x3Y
« Last Edit: November 28, 2010, 01:09:04 PM by Gandolf »

EqwanoX


  • Administrator

  • GMG Extraordinaire

  • *****


  • Posts: 1180
Re: Arrow Keys
« Reply #13 on: November 28, 2010, 03:51:52 PM »
oh i see now, i never noticed that before. gandolfs solution works pretty good. i tweaked the code and time limits a little. this would be a good thing to mention to mike, theres a mouseup so it should be easy to add a keyup.

key down:
Code: [Select]
IF moveCount = 0 THEN LET moveCount = 30
IF moveCount > 0 THEN LET moveCount = 10
IF LASTARROW$ <> KEY$ THEN LET moveCount = 30
LET LASTARROW$ = KEY$
open card:
Code: [Select]
CREATESPRITE 1, "x"
MOVESPRITE 1, 200, 200

ON TIMER 1
 Â  IF moveCount > 0 THEN
 Â     IF KEY$ = UPARROW$ THEN PUSHSPRITE 1, 0, -2
 Â     IF KEY$ = DOWNARROW$ THEN PUSHSPRITE 1, 0, 2
 Â     IF KEY$ = LEFTARROW$ THEN PUSHSPRITE 1, -2, 0
 Â     IF KEY$ = RIGHTARROW$ THEN PUSHSPRITE 1, 2, 0
 Â     LET moveCount = moveCount - 1
 Â  END IF
END TIMER
« Last Edit: November 28, 2010, 03:55:04 PM by EqwanoX »

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Arrow Keys
« Reply #14 on: November 28, 2010, 04:15:35 PM »
Quote
I've just tried your code in Sc and it turns out that KEY$ stays equal to the last key pressed until a new key is pressed. It's unfortunate.
That's how GM works; my example is how to work around it. ;D
I survived the spammage of 2007