Hidro
Hidro

Reputation: 41

Zoom in/out limits on AS3 for iOS programming

I'm having a trouble with my code. I've made the basic zoom with AS3, using the two fingers to zoom it. But I have a trouble;

I need the zoom in stop in 2 for example (the normal size is 1), and then, I need to zoom out max to 1. Here is my code, but if I zoom fast, the zoom goes more than 2.

I need to limit the zoom, between 1, and 2.

Multitouch.inputMode = MultitouchInputMode.GESTURE;

escenario.addEventListener(TransformGestureEvent.GESTURE_PAN, fl_PanHandler);

stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM, fl_ZoomHandler);

function fl_PanHandler(event:TransformGestureEvent):void
{

    event.currentTarget.x +=  event.offsetX;
    event.currentTarget.y +=  event.offsetY;

}

function fl_ZoomHandler(event:TransformGestureEvent):void
{

    if (event.scaleX && event.scaleY >= 1 && escenario.scaleX && escenario.scaleY <= 2) 
    {

        escenario.scaleX *=  event.scaleX;
        escenario.scaleY *=  event.scaleY;
        trace(escenario.scaleX);

    }

}

Upvotes: 0

Views: 202

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

Since you're doing a times/equals (*=) your value can easily go above the threshold of 2 in your if statement since you are multiplying that value after the if statement. You could just do this:

function fl_ZoomHandler(event:TransformGestureEvent):void {
    var scale:Number = escenario.scaleX * event.scaleX; //the proposed new scale amount

    //you set both the scaleX and scaleY in one like below:
    escenario.scaleY = escenario.scaleX = Math.min(Math.max(1,scale), 2);

    //^^^^  inside the line above,  
       //Math.max(1, scale) will return whatever is bigger, 1 or the proposed new scale.   
       //Then Math.min(..., 2) will then take whatever is smaller, 2 or the result of the previous Math.max

    trace(escenario.scaleX);
}

Upvotes: 1

Related Questions