Post new topic    
Liquid Metal Slime
Send private message
Decrementing a variable during an on key press script 
 PostMon Jan 11, 2010 6:42 am
Send private message Reply with quote
This is a portion of the script. Let me know if more needs to be seen:

Code:
               
if (key is pressed(45)) then (
if (alreadyquacked == false) then (
if (staminasize >> 0) then (
play sound (7,false,false)
free slice (staminabar)
staminasize -= 5
staminabar := create rect(staminasize, 10)
put slice (staminabar,60,20)


When you press key 45, it is only supposed to take -5 each time it is pressed. However, it completely drains it unless you are very light on touching the key. Is there a way to make sure it only reduces the amount by 5 each time it is pressed?

FYI, I used to know the answer to this because I did the same thing in Mikey T. Unfortunately, I lost the script for that game some time ago though.
Cornbread Chemist
Liquid Metal King Slime
Send private message
 
 PostMon Jan 11, 2010 4:06 pm
Send private message Reply with quote
Keyval will help here

Code:

if (keyval(45) >> 1) then (


This will not repeat so fast, although it will still be possible to hold down the key and reduce stamina fast.


If you really want to make sure that each keypress is only handled once with NO repeats for being held down, then you need to use a global variable.

Code:

if(key is pressed(45) && stamina key==false) then(
  stamina key := true
  # the rest of the stuff that happens for stamina
)else(
  stamina key := false
)
Super Slime
Send private message
 
 PostMon Jan 11, 2010 4:26 pm
Send private message Reply with quote
Slight problem with James's code:

Code:
if(key is pressed(45) && stamina key==false) then(
  stamina key := true
  # the rest of the stuff that happens for stamina
)else(
  if (key is pressed(45) == false) then (stamina key := false)
)


The second-to-last line needs to check if the key is still held down; otherwise, you're going to have the same problem at half the rate.
Mega Tact v1.1
Super Penguin Chef
Wizard Blocks
Liquid Metal King Slime
Send private message
 
 PostMon Jan 11, 2010 4:30 pm
Send private message Reply with quote
Oh, right. I guess what I meant was:

Code:

if(key is pressed(45)) then(
  if(stamina key==false) then(
    stamina key := true
    # the rest of the stuff that happens for stamina
  )
)else(
  stamina key := false
)


but Mogri's way is good too.
Liquid Metal Slime
Send private message
 
 PostMon Jan 11, 2010 7:06 pm
Send private message Reply with quote
Thanks for the help! I knew it was a quick fix V
Cornbread Chemist
Display posts from previous: