There are two ways to make a scrollable image in the AS3 mobile project: using Pan gestures (with two fingers) or using the mouse down, move and up (events with one finger are registered as mouse events on mobile devices). The example below supports both.
Note. With mouse events, you might want to add a timer to distinguish between clicks and drags (shown in the example).
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.events.TransformGestureEvent;
import flash.geom.Point;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.utils.getTimer;
public class ScrollExample {
private var t:DisplayObject;
private var _prevX:Number;
private var _prevY:Number;
private var _dragging:Boolean = false;
private var _lastMouseEvent:int;
private static const MIN_DRAG_TIME:Number = 150;
public function ScrollExample() {
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_PAN, onPan);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMoveDrag);
stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
}
private function onPan(e:TransformGestureEvent):void {
t.y += e.offsetY;
}
private function onStartDrag(e:MouseEvent):void {
_lastMouseEvent = getTimer();
_dragging = true;
_prevY = e.stageY;
}
private function onMoveDrag(e:MouseEvent):void {
if (getTimer() > _lastMouseEvent + MIN_DRAG_TIME && _dragging) {
t.y = ValidYDragPosition(e);
_prevX = e.stageX;
_prevY = e.stageY;
}
}
private function onStopDrag(e:MouseEvent):void {
_dragging = false;
if (getTimer() <= _lastMouseEvent + MIN_DRAG_TIME) {
onClick(e);
}
}
private function onClick(e:MouseEvent):void {
}
private function ValidYDragPosition(e:MouseEvent):Number {
var requestedPoint:Number = _prevY - e.stageY;
if (t.y - requestedPoint > 0) {
return 0;
} else if (t.y - requestedPoint < stage.stageHeight - t.height) {
return stage.stageHeight - t.height;
} else {
return t.y - requestedPoint;
}
}
}
source
share