Actionscript 3缩放/缩放不起作用

时间:2014-02-22 19:21:11

标签: actionscript-3 scaling zooming

我尝试根据本教程创建缩放功能:link

但是,当我尝试缩放bg_image没有任何事情发生时。这就像我无法访问它的属性。我认为这与我在init函数中将bg_image作为子项添加到spImage的行相关。 (`spImage.addChild(bg_image))

在此行之前,我可以更改bg_images属性,这是我无法完成的。所以我猜这个问题与访问另一个movieclip的孩子有关。

有谁知道如何解决这个问题? :)

泰!

主要课程

package 
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.text.TextField;
import fl.controls.Button;
import fl.controls.List;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;

import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Mouse;

public class Main extends Sprite
{
    // Zoom

    public static var scale:Number = 1;

    public var spImage:Sprite;
    public var mat:Matrix;
    public var mcIn:MovieClip;
    public var mcOut:MovieClip;
    public var boardWidth:int = 980;
    public var boardHeight:int = 661;

    public var boardMask:Shape;

    public var externalCenter:Point;
    public var internalCenter:Point;

    public const scaleFactor:Number = 0.8;

    public var minScale:Number = 0.25;
    public var maxScale:Number = 10.0;

    // ------------------------

    public var bg_image:Sprite;
    //-------------------------------

    public function Main()
    {

        addEventListener(Event.ADDED_TO_STAGE, init);

    }

    public function init(e:Event):void
    {
        bg_image = new Image(0, 0);
        this.addChild(bg_image);

        bg_image.addEventListener(MouseEvent.CLICK, mouseCoordinates);

        removeEventListener(Event.ADDED_TO_STAGE, init);

        // Zoom

        this.graphics.beginFill(0xB6DCF4);
        this.graphics.drawRect(0,0,boardWidth,boardHeight);
        this.graphics.endFill();

        spImage = new Sprite();
        this.addChild(spImage);

        boardMask = new Shape();
        boardMask.graphics.beginFill(0xDDDDDD);
        boardMask.graphics.drawRect(0,0,boardWidth,boardHeight);
        boardMask.graphics.endFill();
        boardMask.x = 0;
        boardMask.y = 0;
        this.addChild(boardMask);
        spImage.mask = boardMask;

        minScale = boardWidth / bg_image.width;

        mcIn = new InCursorClip();
        mcOut = new OutCursorClip();

        bg_image.addChild(mcIn);
        bg_image.addChild(mcOut);

        bg_image.scaleX = minScale;
        bg_image.scaleY = minScale;

        spImage.addChild(bg_image);
        spImage.addChild(mcIn);
        spImage.addChild(mcOut);

        spImage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
        stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

        spImage.addEventListener(MouseEvent.CLICK, zoom);

        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);

    }

    private function startDragging(mev:MouseEvent):void
    {
        spImage.startDrag();
    }

    private function stopDragging(mev:MouseEvent):void
    {
        spImage.stopDrag();
    }


    private function zoom(mev:MouseEvent):void
    {
        if ((!mev.shiftKey)&&(!mev.ctrlKey))
        {
            return;
        }
        if ((mev.shiftKey)&&(mev.ctrlKey))
        {
            return;
        }

        externalCenter = new Point(spImage.mouseX,spImage.mouseY);
        internalCenter = new Point(bg_image.mouseX,bg_image.mouseY);

        if (mev.shiftKey)
        {
            bg_image.scaleX = Math.max(scaleFactor*bg_image.scaleX, minScale);
            bg_image.scaleY = Math.max(scaleFactor*bg_image.scaleY, minScale);
        }
        if (mev.ctrlKey)
        {
            trace("Minscale: ", maxScale)
            trace("Returned: ", 1/scaleFactor*bg_image.scaleY)
            bg_image.scaleX = Math.min(1/scaleFactor*bg_image.scaleX, maxScale);
            bg_image.scaleY = Math.min(1/scaleFactor*bg_image.scaleY, maxScale);
        }

        mat = this.transform.matrix.clone();

        MatrixTransformer.matchInternalPointWithExternal(mat,internalCenter,externalCenter);

        bg_image.transform.matrix = mat;
    }

    private function keyHandler(ke:KeyboardEvent):void
    {

        mcIn.x = spImage.mouseX;
        mcIn.y = spImage.mouseY;
        mcOut.x = spImage.mouseX;
        mcOut.y = spImage.mouseY;

        mcIn.visible = ke.ctrlKey;
        mcOut.visible = ke.shiftKey;

        if (ke.ctrlKey || ke.shiftKey)
        {
            Mouse.hide();
        }
        else
        {
            Mouse.show();
        }
    }

    private function reise(evt:MouseEvent):void
    {
        var new_flight:Flight = new Flight(airportDict[startList.selectedItem.label]["x"],airportDict[startList.selectedItem.label]["y"],airportDict[sluttList.selectedItem.label]["x"],airportDict[sluttList.selectedItem.label]["y"]);
        bg_image.addChild(new_flight);
    }

    private function mouseCoordinates(event: MouseEvent):void
    {
        // these are the x and y relative to the object
        var localMouseX:Number = bg_image.mouseX;
        var localMouseY:Number = bg_image.mouseY;
        trace("Local coordinates: ", localMouseX, localMouseY);

        // these are the x and y relative to the whole stage
        var stageMouseX:Number = event.stageX;
        var stageMouseY:Number = event.stageY;
        trace("Global coordinates: ", stageMouseX, stageMouseY);
    }
}

}

图片类:

package  {
import flash.display.Sprite;
import flash.display.MovieClip;


public class Image extends Sprite
    {

    public function Image(y_:Number, x_:Number) 
    {
        this.y = y_
        this.x = x_
    }

}

}

0 个答案:

没有答案