Game Development Reference
In-Depth Information
nal size of the application code making it especially handy for download over limited
network connections, such as those found in most mobile devices today.
Track touches by IDs
The way we wrote our component to handle user input through touch makes the as-
sumption that only one touch will be used at all times. While this assumption may
hold true for most of the time in our game, it may not be the case in other games.
The TouchDrag component always looks for the touch information on the very first
touch object found within the changed touches list. The only issue with that is that
the original touch may not always be the first array element within its parent array.
To change this, all we need to do is keep track of the touch ID of the finger that first
touches the screen, then reference that touch based on its identification value.
Packt.Components.TouchDrag = function(entity,
canvas) {
var touchId = 0;
// When a successful touch is first captured,
cache the touch'sidentification
function doOnTouchDown(event) {
event.preventDefault();
var phy = entity.getComponent("physics");
var touch = event.changedTouches;
if (phy) {
touchId = touch.identifier;
isDown =
phy.collide(touch[touchId].pageX,
touch[touchId].pageY, 0, 0);
}
}
// Clear the touch flag on the entity, as
well as the touch id
function doOnTouchUp(event) {
event.preventDefault();
Search WWH ::




Custom Search