Monday, July 19, 2010

iPhone touch motion

View

import flash.events.MouseEvent;
import flash.events.Event;

var totalScene:uint = 4;
var thumbWidth:Number = 317;
var currentIndex:int = 0;
var startMoveX: Number;
var lastMoveX: Number;
/*var startMoveY: Number;
var lastMoveY: Number;*/

var isDragging: Boolean = false;
var desX:Number = 0;

// FOR TEST ON MOUSE

//scene_mc: a top-left movie, contains 4 images with image's width = 317
scene_mc.addEventListener(MouseEvent.MOUSE_DOWN, onSceneMouseBegin);
this.stage.addEventListener(MouseEvent.MOUSE_UP, onSceneMouseUp);
function onSceneMouseBegin(e: MouseEvent)void {        
    isDragging = true;
    startMoveX = this.mouseX;
    //startMoveY = this.mouseY;
    
    scene_mc.startDrag(falsenew Rectangle(-scene_mc.width + thumbWidth/20
                        scene_mc.width, 0));
    this.removeEventListener(Event.ENTER_FRAME, ef);
    this.addEventListener(Event.ENTER_FRAME, ef);    
}

function onSceneMouseUp(e: MouseEvent)void {        
    scene_mc.stopDrag();
    isDragging = false;

    var diff = lastMoveX - startMoveX;
    if (Math.abs(diff30) {
        if (diff > 0) {
            activeSlide(currentIndex - 1);
        else {
            activeSlide(currentIndex+1)
        }
        return;
    }
    
    //check pos x if out of range
    var posX: int = Math.abs(scene_mc.x / thumbWidth);
    var plusX: Number = Math.abs(scene_mc.x % thumbWidth);    
    if (Math.abs(plusX>= thumbWidth/2posX ++;    
    if (posX >= totalSceneposX = totalScene - 1;
    activeSlide(posX);
}

function getMouseY(): Number {
    return this.mouseY;
}
function ef(e: Event)void {
    lastMoveX = this.mouseX;
    //lastMoveY = this.mouseY;
    if (!isDraggingscene_mc.x += (desX - scene_mc.x)*0.15;
}

function activeSlide(index: int) {
    if (index < 0index = 0;
    if (index >= totalSceneindex = totalScene - 1;
    currentIndex = index;
    desX = -index*thumbWidth;
}

No comments: