The phaser ce inputEnabled component is used in most game display objects including sprites to allow for input handing in relation to the display object. These are many instances of a Signal than can be used to attach event handers that will fire when a player does something such as clicking or touching the display object. This post will serve as an overview of the input enabled component in phaser ce, and I will like to other relevant posts on handing input in a phaser ce project where appropriate.
There are also other ways of handing user input that may be a better choice depending on the nature of the game that is being developed. The events that can be used with input enabled will work okay for some projects, however other projects might call for other ways of handing user input that involve directly polling the state of input devices. It is possible to poll pointer objects in general such as with active pointer, touch pointers, and then there is also of course the keyboard and mouse pointer when making desktop centric style games. A good starting point might be to check out the main input handler thought.
In Order to do much of anything involving attaching input handers to a certain display object like a sprite, the display object must support the inputEnabled component. Many of the display objects do including graphics, and sprites. So if the object does include the input enabled component, to get started the inputEnambed boolean must be set to true.
Once the the inputEnabled bool of the display object is set to true, the input property of the display object will have a value that is an instance of inputHandler rather than null. In addition any event handers added with the events component, will now work. Be sure to also read my post on the events component as well for a detail overview of what there is to work with.
In this example I will create an instance of phaser graphics, a kind of display object in phaser that includes the input enabled component that can be used to produce on the fly graphics. When the graphics object is clicked the, the color of the graphics object will change thanks to some events that can now be used once input is enabled.
So for this example I start off by making some helper methods that will then be used when creating a state object later on. The mkGraphic method that I made just creates an instance of phaser graphics, and then appends it to an game.data object that I use to store references to things like display objects. In this method I also make sure to set the inputEnabled boolean of the graphics object to true, this will allow certain methods to now be used to handle input for the sprite.
The draw method will be called once the example starts, and again each time the sprite is clicked or touched. So this is what will be called to change the state of the graphics based on user input. In a real project much more could be done when the player interacts with a display object, but for this simple hello work style example I will just be changing the color of a graphics object.
So this method will attach events to the graphics object that should now work once input has been enabled for it. Here I use the onInputDown signal to change the color by calling my draw method, and then set it back thanks to onInputUp.
Now to make the example come to life by creating an instance of Phaser.Game, and making a single state object. Because this example is completely event driven there is no need for an update method. I start by creating my game.data object in the create method of the state, in more complex projects there will be more than one value attached other than the graphics object, I always make it though it a lot of these examples because it is just a practice I picked up.
I then create the graphics object, which will also enable input, at which point it is now cool to attach some events, and then I finish my calling the draw method once to set the initial state of the graphics object.
If all goes well this results in a simple example the will chance color when the graphics is clicked. Nothing to exciting, but if you are looking for a simple hello world style example I cant make things much more simple than that. In a real project this might be used as a way to make an enemy display object take damage when clicked when making a defense strategy type game for example.
So the basic idea here is to just set the inputEnabled boolean of a display object like a sprite, or graphics object to true. After doing that it is possible to attach handlers to certain events in the events component of the display object that can be used to preform actions on a per event type basis. If you would like to see more examples, or have any questions be sure to let me know in the comments section. I have been focusing on making my phaser content more solid and would like to here from my readers. In any case be sure to have fun when working on your own projects, this is a game framework after all.