Activating an event action on a second button press

This forum is currently in read-only mode.
From the Asset Store
Very simple code without excess options (15 events for server and 11 events for client)
  • 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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

  • 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

  • You should get to know private and globals really well.

    They're essential, really, and easy once you've used them once or twice.

    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:

    <img src="http://farm5.static.flickr.com/4010/4669662881_b9f1daba2c_b.jpg">

    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.

  • 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.

    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.

  • > 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.

    >

    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.

    Ah, gotcha. GV it is.

    Thanks guys.

  • 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.

    right, 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

  • 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):

    http://dl.dropbox.com/u/5868916/Choices.cap

    Here's a slightly abbreviated text version of the main events:

    + 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:
    [ul]
    	[li]I used [i]On key pressed[/i] instead of [i]On key down[/i]. If I had used [i]On key down[/i], I would have had to add [i]System: Trigger once while true[/i] to that condition to keep it from toggling for every tick that the key is down. [i]On key pressed[/i] will only be true for the duration of the tick that is is detected in.[/li]
    	[li]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.[/li]
    	[li]I used the [i]System: Else[/i] condition instead of an inverted condition: [i](NOT) Is Shaking[/i]. 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...[/li]
    [/ul]
    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.
  • 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

  • Great minds at work

    Thanks for all the info and examples, guys! I can use a lot of this stuff in the future, too.

    @Lucid...I understand what you're saying now. That's a good tip. I have the global variable in already, but I tested your example and it works well too. I'm thinking about doing a mini-game where I would use a similar approach. I'm going to hide things under items and have the user find it by destroying the front sprite.

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