Click Here for an Incredible Phaser Button Animation You Wont Believe What Happens Next
Phaser 2 had buttons but they aren’t built in to Phaser 3. In reality, they are simple to make on our own. In this tutorial we’ll break down what it means for something to be a button and how to make one in Phaser-land.
Hover states, animations, fancy effects, whatever, are just icing on the cake. Phaser makes it really easy to make something clickable, so let’s start with building that.

I’ll be using the project setup from this blog post. You can follow through that post to set up the project yourself, or you can clone the full setup from GitHub and follow the instructions in its README.
Phaser 3] Doctor Simon's Time Crash
Lucky for us our project already has taken care of the first part of our most basic button. That ugly green text on that ugly black background? Looks like a perfect candidate for an ugly button!.
Class. It’s this class that provides us with the method we need to make our text (or image, or sprite, or any other
Method. Don’t worry about any of the arguments to the method for now. If we call that method on our text object with no parameters, it will make the rectangular bounds of our text interactive, which is exactly what we want.
Html Buttons In Phaser 3
. We’ll also keep a reference to our text object in a variable so we have access to it later. Since we don’t need the coke can sprite, we can remove that as well.
Great! But it doesn’t seem like anything has changed in our browser. We can check that our text is interactive by adding event listeners to it. When we tell Phaser a
And not for the scene in general, we’ll only listen to events coming directly from our text and ignore those coming from the
Css For Button Glow Effect On Hover
Great, we know our “button” is firing off input events that we can listen for and respond to. The other events that we care about are:
The Most Basic Button™ only cares about when the user clicks on it, so to start we’ll only need to listen to the
Event. Let’s set up our scene to have have a click counter that increases everytime we click on our button. We’ll change our
Phase • Animations Made Easy!
Didn’t it feel weird when you moused over The Most Basic Button™ and nothing changed visually? This is because you were expecting the button to have some kind of hover state. We can detect when a user is hovering over a button with the
Event. Let’s have our button change its text color when the user hovers over it. We can add listeners for those two events to change our button’s color. We’ll also need to save a reference to our button in the scene so we can call the

Similarly, our Slightly More Complicated Button™ should have an active state too. Which gives our button three different states in total: rest, hover, and active. Let’s explicitly call out how we will transition between all our states:
How To Remake Mario In Phaserjs: Part I.
As of version 3.10.0, Phaser makes it very easy for us to change the mouse cursor to a hand during the hover state of our button. Instead of calling
In the spirit of keeping our codebase well organized, we have some refactoring to do. If we want to add more than one of our ugly buttons to our game, we would need to duplicate this code each time. Instead of that, let’s extract the code for the button into its own class.
Bonus Tip: Phaser has an excellent plugin system that allows you to add your own GameObject factories. But honestly, I haven’t taken the time to dive into it yet, plus its a little outside the scope of this tutorial. As an exercise on your own, try and build this out into a real plugin! A Custom GameObject
Run Timeline Animation On Click
Let’s keep it simple and stick with a text-only button. Make a new folder in your project to keep our custom game object in:
Congratulations! You’ve created your first custom GameObject. Granted, it doesn’t have any custom functionality - it’s just a renamed text object at this point. But we may as well start using it in our refactoring process.
We’ll need to slightly adjust how we’re creating our text button as well. Since there is no factory method for our custom game object, we’ll have to instantiate it manually and then add it to the scene. Let’s start by doing that with the regular Phaser text object.
How To Code The “game Of Life” With React In Under An Hour
, let’s start baking in some of its functionality. We’ll hardcode it to start, then allow the button to be configured through some parameters in its constructor.
We can start by having the button automatically set itself as interactive. We want to do this when the button is instantiated, so we’ll have to override the constructor for the button.
’s constructor prior to our custom functionality. This way, the Text object gets created correctly in Phaser. So we take in the same parameters that we are currently passing to the Text object’s constructor, and just forward them along in ours.
How Am I Supposed To Respawn When I Can't Click The Button? And No, It Wouldn't Let Me Pick Up Loot To Clear The Buttons Out, Either.
Still works! This is a good example of how refactoring generally goes. Build something out, then pull out the related parts into their own components.
Awesome! Our code in the Scene is looking much cleaner already - the code in the scene doesn’t have anything to do with changing the styles in our

Event to be the appropriate time to respond to the user’s click. Having this knowledge abstracted away from our Scene and into the
Phaser Game Prototyping By Stephen Gose [leanpub Pdf/ipad/kindle]
Makes it a more reusable and simpler component. To fix this, we can pass in a callback function as a parameter to the
Class. It knows how to change transition between its interaction state, as well as when to call the callback function that’s passed in through its constructor. What that callback does is not a concern to the
So let’s put all this abstraction to good use. Let’s add another button to our scene that will subtract one from our click count. We’ll rework some of the code in our Scene here as well:
How To Create Sprite Sheets And Animations With Monogame
Only concerns itself with being a button, and our Scene only concerns itself with the actual scene logic, we were able to reuse our custom game object really easily.
You can find the full source of this example up on github. Phaser also has a great set of examples for interacting with the input systems. Take a look at these three examples for the same pointer events we used for our button: pointerup, pointerdown, pointerover and pointerout.

That respond to pointer events. That means that rather than a Text object, you could also make a button out of a Sprite, or an Image, or any other game object!
Creating A Phaser 3 Template
I hope that the exercise in refactoring was helpful too. This is a good pattern to follow for building out different components of your game. Build it and make it work in your scene, refactor it out so that its logic is encapsulated to its own file. Clean coders will tell you this is keeping a good Separation of Concerns in your project.
Belum ada Komentar untuk "Click Here for an Incredible Phaser Button Animation You Wont Believe What Happens Next"
Posting Komentar