The
Touch object detects input from touchscreen devices like iPhones, iPads and Android phones. Touchscreens for desktop computers are also available, but these are rare compared to mobiles, so generally the Touch object is used in anticipation of input on a mobile device.
The Touch object also provides input from the
accelerometer and
gyroscope (tilt/compass direction) if the device supports them.
It is
best to support touch input wherever possible. On the modern web many users are now browsing on mobile devices with touch input only and no mouse or keyboard. If your game does not support touch controls, many users will be unable to play your game at all. For a guide on how to implement on-screen touch controls, see the tutorial on
Touch controls.
Multi-touch
The Touch object supports multi-touch. This is most useful with the
On touched object and
Is touching object conditions, which can for example detect if multiple on-screen touch controls are being used. This is sufficient for most games. However, the expressions to return the X and Y co-ordinates of the touch only can return the result of a single touch. It is therefore not recommended to rely on these expressions for multi-touch games.
Touch properties
Use mouse inputIf set to
Yes, mouse clicks will simulate touch events. Clicking and dragging the left mouse button will simulate a touch along where mouse dragged, and single clicks will simulate taps. This can be very useful for testing touch events work properly on a desktop computer with no touch input supported. However, only single-touch input can be simulated with a mouse, and a mouse is much more precise than a touch, so it is still best to test on a real touchscreen device.
Touch conditions
Device motion supportedTrue if the device supports motion detection (the accelerometer).
Device orientation supportedTrue if the device supports orientation detection (compass/tilt).
Is in touchTrue if anything is currently touching the screen.
Is touching objectTrue if any touch is currently touching a given object.
On touch endTriggered when any touch releases from the screen.
On touch startTriggered upon any touch on the screen.
On touched objectTriggered when a given object is touched.
Touch actions
The Touch object has no actions.
Touch expressions
AccelerationXAccelerationYAccelerationZGet the current device's motion as its acceleration on each axis in m/s^2 (meters per second per second) excluding the effect of gravity. The expressions which include gravity (below) are more widely supported; these will return 0 at all times on devices which do not support them.
AccelerationXWithGAccelerationYWithGAccelerationZWithGGet the current device's motion as its acceleration on each axis in m/s^2 (meters per second per second) including the acceleration caused by gravity, which is about 9.8 m/s^2 down at all times. For example, at rest, the device will report an acceleration downwards corresponding to the force of gravity. These expressions are more commonly supported than the expressions returning acceleration without G (above). However, devices are still not guaranteed to support motion detection, in which case these will return 0 at all times.
AlphaBetaGammaReturn the device's orientation if supported, or 0 at all times if not suppoted.
Alpha is the compass direction in degrees.
Beta is the device front-to-back tilt in degrees (i.e. tilting forwards away from you if holding in front of you). A positive value indicates front tilt and a negative value indicates back tilt.
Gamma is the device left-to-right tilt in degrees (i.e. twisting if holding in front of you). A positive value indicates right tilt and a negative value indicates left tilt.
AbsoluteXAbsoluteYReturn the current position of a touch over the canvas area in the HTML page. This is (0, 0) at the top left of the canvas and goes up to the window size. It is not affected by any scrolling or scaling in the game. These can only return the position of a single touch (if any), so these are not recommended for use in multi-touch games.
XYReturn the current position of a touch in game co-ordinates. This is (0, 0) at the top left of the layout. It changes to reflect scrolling and scaling. However, if an individual layer has been scrolled, scaled or rotated, these expressions do not take that in to account - for that case, use the layer versions below. These can only return the position of a single touch (if any), so these are not recommended for use in multi-touch games.
X("layer")Y("layer")Return the current position of a touch in game co-ordinates, with scrolling, scaling and rotation taken in to account for the given layer. The layer can be identified either by a string of its name or its zero-based index (e.g.
Touch.X(0)). These can only return the position of a single touch (if any), so these are not recommended for use in multi-touch games.