Game Development Reference
In-Depth Information
mPosition = new Point();
addEventListener( TouchEvent.TOUCH_BEGIN,
onTouchBegin, false, 0, true );
addEventListener( TouchEvent.TOUCH_MOVE,
onTouchMove, false, 0, true );
addEventListener( TouchEvent.TOUCH_END, onTouchEnd,
false, 0, true );
addEventListener( MouseEvent.MOUSE_DOWN,
onMouseDown, false, 0, true );
addEventListener( MouseEvent.MOUSE_MOVE,
onMouseMove, false, 0, true );
addEventListener( MouseEvent.MOUSE_UP, onMouseUp,
false, 0, true );
public function init( _coordinateSpace:DisplayObject
Container, _initialPosition:Point, _distanceThreshold:
Number ):void
mCoordinateSpace = _coordinateSpace;
mPosition = _initialPosition;
mDistanceThreshold = _distanceThreshold;
mTouching = false;
This class needs to keep track of a few things. One is the touch
position of the user
s finger, stored in mPosition . It also keeps track
of the coordinate space to translate the touch position, as having
the position relative to the touch controller itself isn
t likely to be
useful. There is a distance threshold variable, which is used to vali-
date whether the placement of a touch event is close enough to
the point stored in mPosition to update. This will prevent someone
from simply using two fingers to tap opposite sides of the game
board and have their paddle
across the table. When a touch
occurs, if it is outside this distance threshold, it is ignored. The last
property the class stores is a flag to find whether a touch is occur-
ring. This will prevent updates unless someone starts touching
within the distance threshold. These values are all set in the init
function, which we will see later is called by the Game class. You
also notice that in the constructor, we add listeners for both touch
events and mouse events. This is done for the purposes of debug-
ging. Since touch events only work on a device, if you want to test
simple functionality without publishing to that device every time,
you can just export a SWF in the Flash player and the behavior will
be mimicked using the mouse. In practice, it would be a good idea
Search Nedrilad ::

Custom Search