Zooming in and out in AS3

2019-09-16 15:49发布

问题:

I'm making a file where I need to be able to zoom in and out on an Image (Converted to a symbol and given an instance name) using the middle mouse scroller. I've written something similar to :

image1.addEventListener(MouseEvent.MOUSE_WHEEL, function1){
image1 = image 1 +50;
}

so all the scrolling works to increase the image size, but what can I do to make it where if I scroll back the mouse wheel, it scrolls out of the image? From what I understand, there is no converse operation to MOUSE_WHEEL.

回答1:

MouseEvent::delta Indicates how many lines should be scrolled for each unit the user rotates the mouse wheel. A positive delta value indicates an upward scroll; a negative value indicates a downward scroll.

Check if event delta value is positive or negative and scale the image using scaleX and scaleY.

var zoomAmount:Number = 0.1;
stage.addEventListener(MouseEvent.MOUSE_WHEEL, zooom);

function zoom(event:MouseEvent):void {
    if(event.delta > 0) {
        image1.scaleX += zoomAmount;
        image1.scaleY += zoomAmount;
    } else {
        image1.scaleX -= zoomAmount;
        image1.scaleY -= zoomAmount;
    }
}