A working pause button?

Get help using Construct 2

Post » Sat Dec 14, 2013 4:30 pm

I have a global variable called PlayState. I'm using this variable to switch between some of the various states of play.

Now, pressing P during PlayState 1 (the main game) sets PlayState to 2, the pause screen.

Likewise, pressing P during Playstate 2 (the pause screen) sets PlayState to 1, continuing the game.

So I have it set up like this.

System PlayState = 1
      Keyboard On P Pressed: System Set PlayState to 2.

System PlayState = 2
      Keyboard On P Pressed: System Set PlayState to 1.


However this doesn't work. It seems that both conditions end up being true within a single tick, so the game pauses and unpauses before the next frame is drawn. I know this because disabling the second condition causes the pause button to work (but of course it won't unpause).

So how would I get this to work? I considered perhaps adding basic timer variable to force the pause screen to appear for at least a second, but I was wondering if there was a slightly neater way of fixing this.
B
7
S
1
Posts: 12
Reputation: 542

Post » Sat Dec 14, 2013 4:45 pm

I notice people tend to miss this. Code is an In ORder of operation. You must have a data of logic flow. You example actually is very clear on the error made.

if(state = 1) then state = 2
if(state = 2) then state = 1

the Keyboard on pressed state will be TRUE for the entire tick and not just one event. So we can technically just view the two lines of code as always true.

So let's re-examine those lines

Layout start: state = 1;
----
if(state = 1) then state = 2 // now the state is 2
if(state = 2) then state = 1 // since the state was set to 2 in the last line. The condition is now true. Now state =1 is being set.

so in the same tick of Pressed you evaulate both condition as true.

Instead what you want is

Event: If P is Pressed
Sub Event: if state == 1 then state = 2
ELSE
sub Event: if state == 2 then state =1

actually you can skip the if state==2 as the ELSE condition covers that.

Also when you need to pause make sure to set yout timescale to 0 :)
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,028

Post » Sat Dec 14, 2013 5:16 pm

oh. here is an alternative

Event On P Pressed
--Action: set state = (state == 1 ) ? 2 : 1
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,028

Post » Sat Dec 14, 2013 5:19 pm

I actually didn't know there was an ELSE condition in Construct 2. That makes things so much easier. Thanks! :D
B
7
S
1
Posts: 12
Reputation: 542


Return to How do I....?

Who is online

Users browsing this forum: jatin1726 and 31 guests