Showing posts with label touch motion. Show all posts
Showing posts with label touch motion. Show all posts

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(false, new Rectangle(-scene_mc.width + thumbWidth/2, 0, 
                        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(diff) > 30) {
        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/2) posX ++;    
    if (posX >= totalScene) posX = totalScene - 1;
    activeSlide(posX);
}

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

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