Input

In this lesson, we learn about using different kinds of inputs to finally get something interactive!

Mouse
Phaser has an Input class that is responsible for all kinds of input. To access the mouse, we get the input.mousePointer For example:

This would make the object follow the mouse on the x axis. The same applies for the y.

You can get whether the mouse is down or not by accessing the isDown property.

This will be true as long as the mouse is pressed. If you want a callback instead, the Mouse class provides a few useful callbacks.

This will log only when the mouse is pressed.

Touch
Supporting touch is really easy with Phaser using the pointer class.

By default, Phaser allows 2 pointers, along with the mouse. This means you can have 2 fingers touching the screen at the same time. You can add up to 10 extra pointers. This is a good multi-touch example showing how to add pointers.

You can access the first pointer through:

This gives you whether the pointer is touching the screen or not. You can also get the position and other properties. Check out the pointer class linked to above.

Keyboard
The Keyboard class provides us the functionality we need here. The line of code below will return true when "A" is pressed.

This is the ASCII code. A nice shortcut, instead of having to remember all the ASCII codes, is to just get it from Phaser.

Joystick
Joystick or Gamepad support is not always stable inside browsers, but the Gamepad class provides whatever functionality we might need.

Phaser also has an example of how to implement it.

Challenges

 * Can you make an object appear wherever you click?
 * Can you make an object point towards the mouse?
 * Can you make an object move right/left/up/down with the keyboard?