Key Down vs. Key Pressed

Discussion and feedback on Construct 2

Post » Thu Feb 21, 2013 6:38 am

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)?   
B
22
S
3
Posts: 451
Reputation: 2,135

Post » Thu Feb 21, 2013 6:41 am

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.
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Thu Feb 21, 2013 7:42 am

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

B
22
S
3
Posts: 451
Reputation: 2,135

Post » Thu Feb 21, 2013 7:49 am

Use the ELSE expression:

https://dl.dropbox.com/u/45711709/PauseToggle.capx
B
33
S
6
G
7
Posts: 338
Reputation: 7,424

Post » Thu Feb 21, 2013 8:03 am

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...
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Thu Feb 21, 2013 8:12 am

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
B
22
S
3
Posts: 451
Reputation: 2,135

Post » Thu Feb 21, 2013 8:21 am

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?
B
10
S
3
G
3
Posts: 110
Reputation: 2,206

Post » Thu Feb 21, 2013 9:41 am

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?
B
22
S
3
Posts: 451
Reputation: 2,135

Post » Thu Feb 21, 2013 9:46 am

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.
B
56
S
15
G
10
Posts: 910
Reputation: 11,942

Post » Mon Feb 25, 2013 8:12 pm

[QUOTE=ome6a1717] 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?[/QUOTE]

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.


B
106
S
35
G
37
Posts: 796
Reputation: 25,211

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 14 guests