Key Down vs. Key Pressed

0 favourites
  • 10 posts
From the Asset Store
Piano Keys
$3.99 USD
A musical typing game where the goal is to play a song by pressing the matching keys as fast as you can!
  • I know the general differences between key down and key pressed, but I'm curious as to the reason why something like this wouldn't work:

    If VARIABLE = 0,

    On 1 pressed, set VARIABLE to 1   

    If VARIABLE = 1,

    On 1 pressed, set VARIABLE to 2

    The VARIABLE goes straight to 2. Can anyone explain why this happens, and if it can be averted through on 1 DOWN (and if so, why that is)?

  • The events are evaluate in order. You just set Variable to 1...so when it reads the next event Variable = 1 and it results in it being true.

  • So what would be the proper way to code something like that?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Use the ELSE expression:

    dl.dropbox.com/u/45711709/PauseToggle.capx

  • Personally, if it's just incrementing the variable each time, I'd do:

    Keyboard: on key pressed 1

    -System: Add to Variable 1

    If you need to limit the variable, you can use the clamp expression. For example, if the highest the value could be is 2:

    Keyboard: on key pressed 1

    -System: Set Variable to Clamp(Variable+1,0,2)

    If you need to roll the variable back to 0 when it goes past 2:

    Keyboard: on key pressed 1

    -System: Add to Variable 1

    System: Compare Variable > 2

    -System: Set Variable to 0

    etc...

  • Its not necessarily that it's incrementing, its just that I have a lot of events that use the same buttons, and trying to contain without creating a billion variables.

    ie. If you hit escape it brings the menu, but if the menu is up, it closes the menu, and if the menu is in the item menu, it goes BACK to the main menu, etc.

    So far I've been able to figure a way through everything, but I just wanted to make sure there wasn't some eureka line of code that would fix all that :)

    Thanks!

    Steve

  • Would it help to just put it the other way around?

    If VARIABLE = 1,

    On 1 pressed, set VARIABLE to 2

    If VARIABLE = 0,

    On 1 pressed, set VARIABLE to 1   

    So it would check if it was 1 before setting the variable?

  • I believe the same thing would happen if you checked for variables first.

    Let me ask you guys this - what if you have TEN different events you want to take place when using the escape key?

    Would you use an else if command for every event?

  • If you have no additional conditions to test once escape has been pressed then you can just lump them all in the same group of actions. If you do have other conditions to test, then you can use sub-events:

    On escape key pressed -> optionally do action(s)

    Check additional condition(s) -> do action(s)

    Check additional condition(s) -> do action(s)

    Check additional condition(s) -> do action(s)

    etc.

  • I believe the same thing would happen if you checked for variables first.

    Let me ask you guys this - what if you have TEN different events you want to take place when using the escape key?

    Would you use an else if command for every event?

    Can you give an example? I found this confusing.. you mean that everytime I pressed the esc key it would behave differently? Is that it?

    In any case, I got it to work here only by changing the order of the numbers, since what theubie said was right.

    <img src="http://i.imgur.com/tNmmY7A.png" border="0" />

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)