Search code examples
actionscript-3flashpinchzoom

Control timeline with pinchzoom in Actionscript 3.0


I'm using Actionscript 3.0 in Flash. Is there a way to control the timeline with pinching (so instead of zooming out/in you are moving the timeline back/forth)? I'm working on an story app where the player is in control of the story.


Solution

  • You could do something like the following:

    import flash.events.TransformGestureEvent;
    Multitouch.inputMode = MultitouchInputMode.GESTURE;
    
    stop();
    
    //listen on whatever object you want to be able zoom on
    stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM , zoomGestureHandler); 
    
    function zoomGestureHandler(e:TransformGestureEvent):void{
        //get the zoom amount (since the last event fired)
        //we average the two dimensions (x/y). 1 would mean no change,  .5 would be half the size as before, 2 would twice the size etc.
        var scaleAmount:Number = (e.scaleX + e.scaleY) * 0.5;
    
        //set the value (how many frames) to skip ahead/back
        //we want the value to be at least 1, so we use Math.max - which returns whichever value is hight
        //we need a whole number, so we use Math.round to round the value of scaleAmount
        //I'm multiplying scaleAmount by 1.25 to make the output potentially go a bit higher, tweak that until you get a good feel.
        var val:int = Math.max(1, Math.round(scaleAmount * 1.25));
    
        //determine if the zoom is actually backwards (smaller than before)
        if(scaleAmount < 1){
            val *= -1;  //times the value by -1 to make it a negative number
        }
    
        //now assign val to the actual target frame
        val = this.currentFrame + val;
    
        //check if the target frame is out of range (less than 0 or more than the total)
        if(val < 1) val = this.totalFrames + val; //if less than one, add (the negative number) to the totalFrames value to loop backwards
        if(val > this.totalFrames) val = val - this.totalFrames; //if more than total, loop back to the start by the difference
    
        //OR
        if(val < 1) val = 0; //hard stop at the first frame (don't loop)
        if(val > this.totalFrames) val = this.totalFrames; //hard stop at the last frame (don't loop)
    
        //now move the playhead to the desired frame
        gotoAndStop(val);
    }