Continuous jumping when holding down the jump key

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
Dettol
Drutt
Posts: 6
Joined: Sun May 01, 2022 4:35 pm

Continuous jumping when holding down the jump key

Post by Dettol »

I am just playing about with the movement commands on a game, left, right, up, down and jump, all fine but we need to really tweak these controls.
I am not keen how my character is able to jump continuous when you hold down the jump key.
Is there a way to stop this, so you have to actually press the key again instead of holding it down?
I was thinking about adding delays or wait, i am just not sure the best and most efficient way to tackle this as each line of code is adding memory.
Any help would be gratefully appreciated.

Code: Select all

IF KEY FIRE
JUMP 13
ENDIF

IF AIRBORNE <> 0               
   IF JUMPSPEED < 50            
      ADD 2 TO JUMPSPEED      
   ELSE
      IF JUMPSPEED > 50        
	  ADD 1 TO JUMPSPEED  
   
      ENDIF
   ENDIF
ENDIF
User avatar
R-Tape
Site Admin
Posts: 6406
Joined: Thu Nov 09, 2017 11:46 am

Re: Continuous jumping when holding down the jump key

Post by R-Tape »

To avoid repeated action you need to debounce the keyread. You have a variable that let's you jump, and is reset when the key isn't pressed. It's more commonly used for firing bullets than jumping, but the code could look something like this (is that a new AGD scripting language btw?):

IF KEY JUMP
IF J=0
JUMP
LET J=1
ENDIF
ELSE
LET J=0
ENDIF
Dettol
Drutt
Posts: 6
Joined: Sun May 01, 2022 4:35 pm

Re: Continuous jumping when holding down the jump key

Post by Dettol »

Hi thanks R tape, but does not seem to effect my jumping style.
Its AGD yes, latest version.
My sprite takes gravity into consideration which would render the game very easy if I cant stop him loop jumping.
I will continue to experiment with your code for sure as I am off for the rest of the day and only did a quick check when i got home.
User avatar
PeterJ
Site Admin
Posts: 6877
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Continuous jumping when holding down the jump key

Post by PeterJ »

Not sure if you use Facebook @Dettol, but there are some good groups that the author of AGD frequents.

https://facebook.com/groups/785775881484393/
User avatar
R-Tape
Site Admin
Posts: 6406
Joined: Thu Nov 09, 2017 11:46 am

Re: Continuous jumping when holding down the jump key

Post by R-Tape »

Dettol wrote: Mon May 02, 2022 12:44 pm Hi thanks R tape, but does not seem to effect my jumping style.
Its AGD yes, latest version.
My sprite takes gravity into consideration which would render the game very easy if I cant stop him loop jumping.
I will continue to experiment with your code for sure as I am off for the rest of the day and only did a quick check when i got home.
That code works on AGD 4.7, though I'm not familiar with anything later than that or MPAGD.

The principle is the same though:
-not pressing the jump key sets the jump variable (e.g. J) to 0.
-pressing the jump key sets J to 1
-if J is 0 and the jump key is pressed, jump is allowed.

So J remains 1 until you release the jump key.
Post Reply