How to: Create action buttons for a mobile UI
7/24/2024
Mihail Todorov
Welcome to our comprehensive tutorial on creating dynamic action buttons for your game! In this guide, we will walk you through the process of designing three distinct types of action buttons: a shooting button, a heavy shot button, and a special attack button. These buttons will enhance your game’s interactivity and provide a more engaging experience for your players.
Previewing the sample
To see the sample live you can find the whole project in the ${Gameface package}/Samples/uiresources/UITutorials/MobileControls
directory.
Creating the base for our buttons
Before we start with each button we’ll make a base style that will serve as the background for all of our buttons.
In our CSS we’ll add
We’ll also be adding a buttons container, that will place all of our buttons in the bottom right of the screen.
and style it
Action buttons
The action buttons will be our shooting and heavy shot buttons.
What we want for these buttons is the ability to swap them out with each other. To do this we’ll need to group them.
So we’ll just add an attack button container and a button base for each and add all of this to the buttons container
And since we want to position the active button to the left and have it bigger than the inactive one, we’ll add the following styles
Where attack-button
is the active and secondary-button
is the inactive one.
The shooting button
The shooting button should fire a shot when pressed, but we want it for the player to be able to choose if they should fire a singe or a burst shot. They can do that by swiping on the button in any direction and it should change the mode.
Setting up the look of the button
To get started we’ll add a container that will not allow the images to overflow so that any images bellow or above will be hidden.
And inside we’ll add a wrapper with our buttons.
The wrapper is the element we’ll be moving when you swipe. Interestingly, even though we only have 2 modes, there are actually 4 buttons. This is because we need to duplicate the first button and place it at the end, and duplicate the last button and place it at the beginning. This way, when you swipe to the end of the modes, there is an extra button that makes it look like you’re seamlessly continuing to the next mode.
We’ll style those by doing the following:
Adding decorations
To indicate that the button can be swiped we can add arrows pointing up and down with an animation
In our html we’ll add the arrows
And style them:
We’ll also create an animation in our style.css
:
and add it to our arrow:
Writing the logic for sliding the buttons to change the mode
We’ll first need to get the wrapper and set the active button so we can follow which is the active mode in our UI.
To change the images on swipe, we’ll add a swipeDown
and swipeUp
function.
To create the illusion of a smooth infinite transition we need to the following:
Where we wait 2 frames for the Layout to happen and the styles to be changed (this is needed when we reset the styles) and then apply the new styles.
Now if we run each of those functions we can see how the modes would change by swiping.
Adding the swiping interaction
But we don’t want our users to run functions, instead we want them to interact with the UI. So to do that we’ll take advantage of the Interaction Manager library and more specifically the touch gestures.
We’ll start by downloading the touch gestures library and adding it to our index.html
and then we can take advantage of the swipe function:
Since the callback itself will provide us with the direction, we’ll need another function to handle it:
And now if we swipe on our UI we can see that modes change.
The heavy shot button
Compared to the shooting button, the heavy shot is more straightforward. Here we only need to add the image:
Which will look like this now:
Swapping the buttons
Swapping the buttons can also be easily achieved by swapping the classes of our buttons like so:
We also add a flag called swipeActive
so that whenever the heavy shot is swapped in, we shouldn’t swipe to change the shooting button. In our handleSwipe
function we can add the following check:
Finally we need to make it so that by double tapping on the buttons they will be swapped. This will be achieved by using the touchGestures from the Interaction Manager library again.
Creating the special attack button
The special attack button will allow users to press and hold it to fill up a bar. When the bar fills it will play an animation.
Setting up the button
To set up the button we’ll do the same as the shooting and heavy shot buttons and make a base with an image inside. And since we want to have a bar filling up we will add an SVG:
Which will result in something like this:
Since by default we want the bar hidden, we’ll add the following style:
Filling up the bar
When the user puts their finger on the button it should start to fill up the bar until it completes, if they lift their finger before the bar completes it should start to gradually empty.
To do that we’ll add a touchstart
and touchend
events to the button
We also need to add the fill of the bar and because the bar needs to fill gradually and then empty, we’ll need to create an interval:
Something to note here is that the initial fill is 1000 and the reason for that is that the stroke-dashoffset
property needs to go from 1000 to 0 to fill up.
We can now add the logic to the touchstart and touchend functions
If we now put and lift our finger from the button we’ll see this:
Adding an animation if the bar is full
Since we want the special ability to activate when the button bar is filled, we also need to add an animation to it.
This will change the color of the special attack button image rapidly and repeat it 10 times.
We have the animation, be we also need to add it when the bar fills. This is why we’ll add a new flag isAnimated
and add the following logic to our functions
Apart from that we also need to watch for when the animation ends so that we can empty the bar:
In conclusion
By following this tutorial, you’ve learned how to create three different types of action buttons, each with unique functionalities. The shooting button offers single and rapid fire shots with a swipe gesture, the heavy shot delivers powerful attacks with a double tap, and the special attack requires a hold to charge. These techniques can be applied to various game genres, providing you with versatile tools to improve your game’s mechanics. Happy coding!