As some of you may know, cluttering control schemes with buttons can become cumbersome to the player. In some instances, it is a necessity, but at other times you want a more fluid way to do things without adding ten extra buttons to your control scheme. One common tactic is to initiate certain actions by double presses of the mouse or other buttons.
In this tutorial, I am going to show you how to emulate double-taps in Stencyl. In fact, you can see an example of something similar–a double click–in my game Pixel Ninja; the large red shuriken is thrown by double-clicking. Though below I will use buttons, clicking can be obtained the exact same way.
STEP 1 : Set up your behavior and attributes
Personally, I would use a separate behavior for double-taps. This will keep any other behavior that needs to use double-taps uncluttered, and is particularly useful if more than one behavior is going to be listening for the double-taps. However, you don’t HAVE to put it in its own behavior. So either you can set up in the appropriate behavior or in a separate, dedicated behavior.
* * * * *
Here are the non-hidden attributes you want:
— Tap Duration (Number) : This number reflects the amount of time you want the first tap to “last”. In other words, how close together do the two taps have to be for it to be considered a double-tap? Put that time (in seconds) here.
— Double Tap Event (Text) : This will be the event that is broadcast when a double-tap occurs.
— Tap Button (Control) : The button for which we want to register double-taps (you can, of course, add more control attributes if you need them and duplicate the logic below for each button).
And the hidden attributes:
— Tap Countdown (Number) : Amount of time left before the first tap is “forgotten”. More on this later; just trust me for now. 🙂
* * * * *
Finally, you will want a “When button is pressed” listener for our Tap Button attribute to go into.
STEP 2 : Defining our logic
There are two scenarios here:
1.) When the button is pressed close enough to a previous button press, thus a double-tap.
2.) When the button is pressed but not close enough to a previous button press (not a double-tap).
Now, let’s elaborate. When these scenarios occur, what actions need to be performed?
Scenario 1:
A.) Begin processing for the new button tap
B.) Project event “Double Tap Event” out into the actor’s other behaviors.
Scenario 2:
A.) Begin processing for the new button tap
Very similar, though action (A) between the two will be a little different (Scenario 2 involves a little more set-up). Let’s get to it.
STEP 3 : Building our logic’s standard decision-making
Essentially, our two main scenarios are opposite conditions (double-tap or not double-tap). So, we will make this into simple “if-otherwise” code.
Now, you may have noticed that we don’t have a boolean attribute here. That’s where our Tap Countdown attribute comes in. If the button is pressed and we are still counting down, that means the two taps are close enough to give us a double-tap. Therefore, we just need to check and see if there is still time left in the countdown or, more technically speaking, see if Tap Countdown is greater than 0.
So, the conditional base for your logic should look like this:
STEP 4 : Building our Tap Countdown timer
Now I will explain in more detail what we are doing with our Tap Countdown attribute. While a tap is being remembered, we are going to give it a number and countdown to 0, the point at which the tap is to be “forgotten” (pressed too long ago to be part of a double tap). We will construct a “Do Every X Seconds” event straight into our code above to do just that.
Here, I have put it into our non-double tap condition; we do not want it in the “double-tap occurred” condition because it will already be going at the time of a double-tap. And of course, let’s set the Tap Countdown attribute so it can start being used to keep track of time remaining.
NOTE: If you want to know more about how this timer event was set up, please view the tutorial “The “Do After” Dilemma : Building Your Own Time Tracking Mechanic”.
Great! Our button tap will now be registered and remembered accordingly, based on the Tap Duration you set in the behavior pane.
STEP 5 : Double Tap Actions
We are almost done, and its pretty simple from here. If the button is pressed and we have a previous tap still being counted down, we have a double tap.
First, send out the event. Simple enough.
Second, reset the tap timer. Doing this will allow your second tap to be remembered for the length specified (Tap Duration), instead of getting canceled pre-maturely when the first tap expires.
And we are pretty much done. However, there is one other thing you might notice: both conditionals set the timer. Instead of having it both places, why not just perform the small optimization of setting it at the end of the conditional logic? It’s going to happen no matter what path is taken. And don’t worry: it won’t affect the first run of the “Do Every X Seconds” loop because that loop waits .1 seconds to execute its first time anyway, meaning that setting the timer will happen beforehand.
* * * * *
And there you have it. Add double taps to your control scheme arsenal today for the low, low price of $0! If you can’t afford this, you may be eligible to apply for a low-interest loan based on your ability to juggle blow torches.
Anyway, happy Stencyling!
Leave a Reply