First, we need to construct a new instance of a touchSprite object.
This is done using methods similar to creating Sprite. For example:
1. var myTouchSprite = new TouchSprite(); A bitmap image can then be dynamically loaded and placed into the touch object "myTouchSprite".
var Loader0:Loader = new Loader();
Loader0.load(new URLRequest("library/assets/cave_art.jpg"));
myTouchSprite.addChild(Loader0); The touchSprite class directly extends the Sprite class, this means that a touchSprite object inherits all of the public properties available to a Sprite object such as the x, y, rotate, scaleX and scaleY display properties.This enables the touch object to be positioned, rotated and scaled then placed on stage. For example:
myTouchSprite.x = 50;
myTouchSprite.y = 150;
myTouchSprite.rotation = 45;
myTouchSprite.scaleX = 0.5;
myTouchSprite.scaleY = 0.5;
addChild(myTouchSprite); As part of GestureWorks3 there are a number of new advanced "automated" methods for handling the transformations of display objects. However in this example we are going to use traditional methods for animating display objects. To do this we will need to turn off some of the new default methods.
myTouchSprite.disableNativeTransforms = true;
myTouchSprite.disableAffineTransforms = true; The first step in adding touch interaction to touch objects is the local activation of all required gestures. To do this we add the gesture (as identified by the "gesture_id" in the GML root document) to the gestureList property associated with the touch object. For example (using the in-line method for adding items to an object list):
Using actionscript to construct touch objects and add gestures allows full control of the touch interactions defined in the root GML document. This efficiently externalizes gesture descriptions and enables further refinement of UI/UX without the need to recompile applications.
GestureEvents are continually dispatched if the match conditions are continually met. If no touch points are detected on a touch object no gesture events are fired. This ensures dormant touch objects do not require unnecessary processing.
Note: In addition to the actionscript methods used in this tutorial there are new "Native" (CML) methods which allow the construction of new touch objects using a secondary external XML document in addition to the GML document.
1. var myTouchSprite = new TouchSprite(); A bitmap image can then be dynamically loaded and placed into the touch object "myTouchSprite".
var Loader0:Loader = new Loader();
Loader0.load(new URLRequest("library/assets/cave_art.jpg"));
myTouchSprite.addChild(Loader0); The touchSprite class directly extends the Sprite class, this means that a touchSprite object inherits all of the public properties available to a Sprite object such as the x, y, rotate, scaleX and scaleY display properties.This enables the touch object to be positioned, rotated and scaled then placed on stage. For example:
myTouchSprite.x = 50;
myTouchSprite.y = 150;
myTouchSprite.rotation = 45;
myTouchSprite.scaleX = 0.5;
myTouchSprite.scaleY = 0.5;
addChild(myTouchSprite); As part of GestureWorks3 there are a number of new advanced "automated" methods for handling the transformations of display objects. However in this example we are going to use traditional methods for animating display objects. To do this we will need to turn off some of the new default methods.
myTouchSprite.disableNativeTransforms = true;
myTouchSprite.disableAffineTransforms = true; The first step in adding touch interaction to touch objects is the local activation of all required gestures. To do this we add the gesture (as identified by the "gesture_id" in the GML root document) to the gestureList property associated with the touch object. For example (using the in-line method for adding items to an object list):
myTouchSprite.gestureList = {"n-drag":true};
This adds the gesture "n-drag" to the touch object* effectively activating gesture analysis on "myTouchSprite"
which inspects the touch object for matching gesture "action" and
calculates point motion. The result is then processed and prepared for
dispatch in the form of a gestureEvent.
myTouchSprite.gestureEvents = true;
The next step is to turn on gestureEvent streaming in the touch object. This enables gestureEvents to be continually**
dispatched when a gesture is recognized on the touch object (as defined
in the GML). At this stage if a listener and handler are not added to
the touch object nothing is done with the gestureEvent. To actively
monitor gestureEvents on the touch object we will need to add an event
listener.
myTouchSprite.addEventListener(GWGestureEvent.DRAG, gestureDragHandler);
This attaches a "DRAG" gestureEvent listener to the touch object and calls the function "gestureDragHandler" when an event is dispatched. The "gestureDragHandler" function is used to define what happens when a "DRAG" gestureEvent is detected on the touch object. For example:
private function gestureDragHandler ():void
{
event.target.x += event.value.dx;
event.target.y += event.value.dy;
}
When this function is called the event values dx and dy are added to the current values of the event target (myTouchSprite)
x and y properties. The effect in this case is to move the touch object
on the x and y axis. When touch points are placed on the touch object
and moved across the screen the touch object is "dragged" in the same direction by the same distance as the average motion of the touch point cluster.{
event.target.x += event.value.dx;
event.target.y += event.value.dy;
}
Using actionscript to construct touch objects and add gestures allows full control of the touch interactions defined in the root GML document. This efficiently externalizes gesture descriptions and enables further refinement of UI/UX without the need to recompile applications.
GestureEvents are continually dispatched if the match conditions are continually met. If no touch points are detected on a touch object no gesture events are fired. This ensures dormant touch objects do not require unnecessary processing.
Note: In addition to the actionscript methods used in this tutorial there are new "Native" (CML) methods which allow the construction of new touch objects using a secondary external XML document in addition to the GML document.