Game Maker's Garage Forum

Game Creation => SilverCreator => Topic started by: Connors on October 20, 2010, 06:28:38 PM

Title: Arrow Keys
Post by: Connors 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.
Title: Re: Arrow Keys
Post by: Gan 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
Title: Re: Arrow Keys
Post by: WarHampster 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
Title: Re: Arrow Keys
Post by: Connors 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...
Title: Re: Arrow Keys
Post by: WarHampster 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 :(
Title: Re: Arrow Keys
Post by: Connors 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.
Title: Re: Arrow Keys
Post by: Charlo 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.
Title: Re: Arrow Keys
Post by: Connors 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.
Title: Re: Arrow Keys
Post by: Silverwind 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.
Title: Re: Arrow Keys
Post by: EqwanoX 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
Title: Re: Arrow Keys
Post by: WarHampster 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.
Title: Re: Arrow Keys
Post by: Silverwind 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
Title: Re: Arrow Keys
Post by: Gan 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
Title: Re: Arrow Keys
Post by: EqwanoX 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
Title: Re: Arrow Keys
Post by: Silverwind 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
Title: Re: Arrow Keys
Post by: GMG Mike on November 28, 2010, 10:01:22 PM
The current keyboard support in SilverCreator is not very good. The Key Down event should only be used for typing (such as the fake edit field in the Palace Chat demo), or situations where a key is pressed only once and not held down.

There are some workarounds. Consider using these functions. They update instantly with true or false when the respective key is held down. You can also hold down, and check, several at the same time.

COMMANDKEY
OPTIONKEY
CONTROLKEY
SHIFTKEY

I will provide more information later. I just got a new Mac Pro* and REALbasic is not set up yet.

* PPC and 10.4 support is not going away because I got a Mac Pro. I do not subscribe to the elitist method of software development.
Title: Re: Arrow Keys
Post by: WarHampster on November 28, 2010, 10:07:50 PM
Quote
I do not subscribe to the elitist method of software development.

(http://forums.tigsource.com/Smileys/derek/hand-clap.gif)
Title: Re: Arrow Keys
Post by: Gan on November 28, 2010, 11:51:11 PM
Quote
I will provide more information later. I just got a new Mac Pro* and REALbasic is not set up yet.
iJealous. :)

Quote
I do not subscribe to the elitist method of software development.
Quote
(http://forums.tigsource.com/Smileys/derek/hand-clap.gif)
That's an awesome smiley.
Title: Re: Arrow Keys
Post by: GMG Mike on November 29, 2010, 12:42:53 AM
Quote
iJealous. :)


The G5 lasted me 6 years and I could have probably gone 2 more except for the Intel only software. I expect similar longevity from this computer. Also got the new 27" display with iSight. The old G5 will be used in the living room now to play movies and do whatever.
Title: Re: Arrow Keys
Post by: Silverwind on November 29, 2010, 04:39:16 AM
Quote

(http://forums.tigsource.com/Smileys/derek/hand-clap.gif)
LOL! I can imagine you having made that Smily Hammy! ;D
Title: Re: Arrow Keys
Post by: WarHampster on November 29, 2010, 07:57:59 AM
Lol, I didn't realize it would be such a hit. You should add it to the forum's smily list.
Title: Re: Arrow Keys
Post by: Connors on December 10, 2010, 07:51:44 PM
Gandolf your delay code is awesome I'll use it for now. (I modified it to slow down gradually so it's smoother with a higher speed)
I just hope SC b3 is released before the end date.