Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [40]

By Root 2547 0
{

var sprite:Sprite = new Sprite();

sprite.graphics.lineStyle(25, Math.random()*0xFFFFFF);

sprite.graphics.drawCircle(0, 0, 80);

sprite.x = event.stageX;

sprite.y = event.stageY;

addChild(sprite);

}

The touchPointID is a new and important event property. Each new touch has a unique ID associated with it, from TOUCH_BEGIN to TOUCH_END. touchPointID gives you a way to identify and store every point and associate data with it if needed.

In this example, we use an Object to store the ID as a property and associate it with a sprite. To drag and drop the sprites, we use the startTouchDrag and stopTouchDrag methods:

var touches:Object = {};

stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);

stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);

function onTouchBegin(event:TouchEvent):void {

var sprite:Sprite = createCircle(event.stageX, event.stageY);

addChild(sprite);

// store the touchPointID and the sprite

touches[event.touchPointID] = sprite;

// drag the sprite

sprite.startTouchDrag(event.touchPointID, true);

}

function onTouchEnd(event:TouchEvent):void {

// retrieve the sprite using the touchPointID

var sprite:Sprite = touches[event.touchPointID];

// stop drag and destroy

stopTouchDrag(event.touchPointID);

sprite.graphics.clear();

removeChild(sprite);

touches[event.touchPointID] = null;

}

function createCircle(x:int, y:int):Sprite {

var sprite:Sprite = new Sprite();

sprite.graphics.lineStyle(25, Math.random()*0xFFFFFF);

sprite.graphics.drawCircle(0, 0, 100);

sprite.x = x;

sprite.y = y;

return sprite;

}

As we discussed earlier, Multitouch.maxTouchPoints determines how many touches a device can support. Many Android devices only support the detection of two simultaneous touch points.

WARNING

A current bug in Android will always report to AIR that two touch points are available, but this will not limit the number of simultaneous touch point events that may be dispatched. For example, if a device supports five touch points, it may still report that it only supports two, but will dispatch events for all five. This bug should be resolved in an upcoming release.

There is no built-in mechanism to prevent a new touch if the maximum has been reached. In fact, expect unpredictable behavior such as the oldest touch no longer functioning. To prevent this, keep a count of how many points are present and stop the code from executing if you have reached the limit:

var pointCount:int = 0;

function onTouchBegin(event:TouchEvent):void {

if (pointCount == Multitouch.maxTouchPoints) {

return;

}

pointCount++;

// create new sprite

}

function onTouchEnd(event:TouchEvent):void {

pointCount--;

// remove old sprite

}

Let’s create an application using touch events and the drawing API. On TouchBegin, a new sprite is created and associated with a touch ID. It draws on TouchMove and is removed on TouchEnd. Draw using two fingers or two separate users:

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

var touches:Object = {};

stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);

stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);

stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);

function onTouchBegin(event:TouchEvent):void {

var sprite:Sprite = new Sprite();

addChild(sprite);

sprite.graphics.lineStyle(3, Math.random()*0xFFFFFF);

sprite.graphics.moveTo(event.stageX, event.stageY);

touches[event.touchPointID] = sprite;

}

function onTouchMove(event:TouchEvent):void {

var sprite:Sprite = touches[event.touchPointID];

sprite.graphics.lineTo(event.stageX, event.stageY);

}

function onTouchEnd(event:TouchEvent):void {

var sprite:Sprite = touches[event.touchPointID];

sprite.graphics.clear();

removeChild(sprite);

touches[event.touchPointID] = null;

}

Other available events are TOUCH_OUT, TOUCH_OVER, TOUCH_ROLL_OUT, and TOUCH_ROLL_OVER.

WARNING

There is a known bug on the Nexus One in which two points that are too close together get swapped or returned reverse values. The issue does not seem to exist on other devices.


Return Main Page Previous Page Next Page

®Online Book Reader