Activating an event action on a second button press

For questions about using Classic.

Post » Fri Jun 04, 2010 4:48 am

I'm using the XBOX 360 controller object. I have a setup like this:

*When I press the 'B' button, an image shakes.

What I want to do is set it so that when I press the 'B' button again it activates an event action (like jumping to a new layout).

I know how to set an event action to make it jump to a new layout under normal circumstances, I just can't seem to figure out how to make it work on the second button press... right now, it jumps to the new layout on the first press.

Do you know what I could be overlooking?
B
12
S
4
G
3
Posts: 426
Reputation: 2,458

Post » Fri Jun 04, 2010 6:19 am

You can make a global variable call it something like "toggle", with an initial value of 0.
Then in your event sheet make two conditions.
Both will compare the variable. The first would compare if the variable is equal to 1 shake the screen, then if the variable is equal to 2 go to layout blah blah.
Finally add an action that adds 1 to the variable each time the button is pressed.
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Fri Jun 04, 2010 2:28 pm

Thanks for the info, newt!

Luckily for me, I just learned how Construct does Global Variables 3 nights ago thanks to a help question someone posted about fixing a timer. It just so happened that I needed to create a timer, so by seeing how he wrote the code I was able to take it and reconfigure it into what I wanted (code examples and .caps are golden).

But I wonder, is there a way to activate an event action on a second button press of the same button without using a Global variable? I only need this action to activate in one screen. You press B and the item shakes infinitely letting you know you've selected it, then you press B again and it jumps to the new layout... or at least that's what I was trying to make it do through events. The shaking part I have, the other part is where the trouble is.
B
12
S
4
G
3
Posts: 426
Reputation: 2,458

Post » Fri Jun 04, 2010 4:25 pm

No, I think you need a global or private variablee.
Unless you want to do something roundabout like
Make sprite1 and sprite2 look the same but be separate objects

Then on click sprite1, create sprite2 in the same location
Start shaking, destroy sprite1

On click sprite2, go to next layout,


but that seems a lot more trouble.
You should get to know private and globals really well.
They're essential, really, and easy once you've used them once or twice.

You're just telling your game to remember something(anything), and giving it a name
Janesphonenumber=123
Call janesphonenumber

Myname="lucid"
Set text to "hi, my name is " + Myname
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Fri Jun 04, 2010 9:52 pm

[quote="lucid":3vcbzp0s]You should get to know private and globals really well.
They're essential, really, and easy once you've used them once or twice.[/quote:3vcbzp0s]

Well, to allude to my earlier post, I know how to do a Global variable as that's how I created my timer. But I'm curious about an alternative because of something I did in the past. In a test I did a few months ago, I was able to set up events to where when I pressed A and B at the same time it would jump to a new layout without using a global variable. So I thought the same type of concept might work for double-pressing a single button, and figured maybe I was just missing something to make it work this tme. But if that's not possible in this specific instance then I have no problem just sticking with a Global variable.

Here is what my events look like without the double-button press code present:



When I click A sprite 23 shakes infinitely, when I click B sprite 23 stops shaking and sprite 24 starts shaking infinitely. This is to identify that the user has chosen the item. Neither sprite is to be destroyed.

The double button press would be to select the item that was chosen.
B
12
S
4
G
3
Posts: 426
Reputation: 2,458

Post » Fri Jun 04, 2010 9:55 pm

[quote="MrMiller":1jqo4jnk]So I thought the same type of concept might work for double-pressing a single button, and figured maybe I was just missing something to make it work this tme. But if that's not possible in this specific instance then I have no problem just sticking with a Global variable.[/quote:1jqo4jnk]

The difference in the double pressing case is you need to keep state. Before you could just check if two buttons were pressed. You need something to keep state and a global or private variable are the easiest ways. There is no good reason not to use them as this is the type of thing they should be used for.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Fri Jun 04, 2010 10:06 pm

[quote="scidave":31y2opxm][quote="MrMiller":31y2opxm]So I thought the same type of concept might work for double-pressing a single button, and figured maybe I was just missing something to make it work this tme. But if that's not possible in this specific instance then I have no problem just sticking with a Global variable.[/quote:31y2opxm]

The difference in the double pressing case is you need to keep state. Before you could just check if two buttons were pressed. You need something to keep state and a global or private variable are the easiest ways. There is no good reason not to use them as this is the type of thing they should be used for.[/quote:31y2opxm]

Ah, gotcha. GV it is.

Thanks guys.
B
12
S
4
G
3
Posts: 426
Reputation: 2,458

Post » Sat Jun 05, 2010 1:09 am

[quote="MrMiller":174z4ss4]

When I click A sprite 23 shakes infinitely, when I click B sprite 23 stops shaking and sprite 24 starts shaking infinitely. This is to identify that the user has chosen the item. Neither sprite is to be destroyed.

The double button press would be to select the item that was chosen.[/quote:174z4ss4]
right, :wink: what I meant was, that while it was possible to do without a global, doing it would be more trouble than it was worth, and I was giving an example of a possible roundabout method of avoiding using a global:

Make another sprite object exactly like the one to be clicked, same image, different name.
when the user clicks, create the second sprite object at the same location as the first, destroy the original, and if the user clicked this new sprite:
OnClick newsprite:
-----Go to next layout
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sat Jun 05, 2010 4:54 am

Hi. This is a common type of scenario that you may run into when developing a game. That is, dealing with discrete states for some aspect of the game. In this case, you'd need to set some sort of 'button pressed once' state when the button has been pressed once, and clear that state if the button is pressed again (as well as taking the desired action), or if the other button is pressed instead.

As has been mentioned, variables are generally used for such a thing. If this is not going to be needed in other layouts, then a global variable would not be needed. In such a case, a private variable could be added to both sprite objects to keep track of the current state of each. However, in your example, the Shake behavior can be used as a simpler solution by using the Is Shaking condition that comes with it. I made a simple example of how I would implement it, here (v0.99.84):

[url:zu6vif0g]http://dl.dropbox.com/u/5868916/Choices.cap[/url:zu6vif0g]

Here's a slightly abbreviated text version of the main events:
[code:zu6vif0g]+ MouseKeyboard: On key 1 pressed
+ Sprite1: Is shaking
-> Text: Set text to " Sprite1"
-> Sprite1: Stop shaking
+ System: Else
-> Sprite1: Start shaking for -1 ms, with intensity 2
-> Sprite2: Stop shaking
+ MouseKeyboard: On key 2 pressed
+ Sprite2: Is shaking
-> Text: Set text to " Sprite2"
-> Sprite2: Stop shaking
+ System: Else
-> Sprite2: Start shaking for -1 ms, with intensity 2
-> Sprite1: Stop shaking[/code:zu6vif0g]

Note a few things about how I set up the events:
- I used On key pressed instead of On key down. If I had used On key down, I would have had to add System: Trigger once while true to that condition to keep it from toggling for every tick that the key is down. On key pressed will only be true for the duration of the tick that is is detected in.
- The state checks are added as sub-events of the triggering action (the key press in this case.) Logically, this is less error prone than trying to make conditions without sub-vents, which can often lead to the undesired result of both states being true in succession, in the same tick, instead of just one.
- I used the System: Else condition instead of an inverted condition: (NOT) Is Shaking. Else is good for checking between two states, as only one will ever be executed in one tick. If you use an inverted version of the first check instead, you can run into the problem of both being true, because the first condition toggles the state, and thus the second is now true because of that...

Anyway, if a variable were used for each choice instead of the Shake behavior, then you'd just need to set it to zero initially, then set it to 1 wherever you had 'Start shaking', and zero wherever you had 'Stop shaking'. Then, replace the 'Is shaking' condition with a check for the variable being equal to 1. Same concept, different method.

I hope this helps. :) I write this as much for others who may read this, having a similar problem. I see this type of problem often.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Sat Jun 05, 2010 6:24 am

There tons of ways to do this, but you have to remember simple is elegant, even if it just feels quick and dirty.

+ System: Start of layout
-> Function: Set return value to 0

+ MouseKeyboard: On Left mouse button Clicked
-> Function: Set return value to Function.Return+1

+ System: Function.Return Equal to 1
-> Sprite: Flash for 5 seconds with 0.05 seconds interval

+ System: Function.Return Equal to 2
-> System: Go to layout 1 with transition "None" lasting 0 MS
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 7 guests