ActionScript 3显然我无法访问movieclip

时间:2014-02-22 13:23:47

标签: actionscript-3 movieclip zooming

我已经创建了一个缩放功能但是当我尝试缩放bg_image没有任何事情发生时。这就像我无法访问它的属性。有谁知道为什么? :)

泰!

主要课程

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;

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

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



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

    public var routeArray:Array;

    public var startList:List = new List();
    public var sluttList:List = new List();

    public var S_Norway:Dictionary = new Dictionary();
    public var S_Australia:Dictionary = new Dictionary();
    public var S_China:Dictionary = new Dictionary();
    public var S_South_Africa:Dictionary = new Dictionary();
    public var S_Brazil:Dictionary = new Dictionary();
    public var S_USA:Dictionary = new Dictionary();
    public var S_France:Dictionary = new Dictionary();

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

    public static var airportDict:Dictionary = new Dictionary();


    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);

        S_USA["x"] = 180.7;
        S_USA["y"] = 149.9;
        S_USA["bynavn"] = "New York";

        S_Norway["x"] = 423.7;
        S_Norway["y"] = 76.4;
        S_Norway["bynavn"] = "Oslo";

        S_South_Africa["x"] = -26;
        S_South_Africa["y"] = 146;
        S_South_Africa["bynavn"] = "Cape Town";

        S_Brazil["x"] = 226;
        S_Brazil["y"] = 431.95;
        S_Brazil["bynavn"] = "Rio de Janeiro";

        S_France["x"] = 459.1;
        S_France["y"] = 403.9;
        S_France["bynavn"] = "Paris";

        S_China["x"] = 716.2;
        S_China["y"] = 143.3;
        S_China["bynavn"] = "Beijing";

        S_Australia["x"] = 809.35;
        S_Australia["y"] = 414.95;
        S_Australia["bynavn"] = "Sydney";

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

        airportDict["USA"] = S_USA;
        airportDict["Norway"] = S_Norway;
        airportDict["South Africa"] = S_South_Africa;
        airportDict["Brazil"] = S_Brazil;
        airportDict["France"] = S_France;
        airportDict["China"] = S_China;
        airportDict["Australia"] = S_Australia;

        for (var k:Object in airportDict)
        {
            var value = airportDict[k];
            var key = k;
            startList.addItem({label:key, data:key});
            sluttList.addItem({label:key, data:key});
            var airport:Airport = new Airport(key,airportDict[key]["bynavn"]);
            airport.koordinater(airportDict[key]["x"], airportDict[key]["y"]);
            bg_image.addChild(airport);
        }

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


    }

    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_
    }

}

}

1 个答案:

答案 0 :(得分:0)

您没有声明minScale,也没有声明maxScale

bg_image应声明为Sprite,否则Compiler Error: Implicit coercion... `

public var bg_image:Sprite;

由于你的图像非常小,你可能想要将你的听众添加到舞台上,而不是图像,点击这么小的图像会非常困难。

此外,以下是使用MOUSE_WHEEL的示例,而不是单击 + Ctrl / Shift

stage.addEventListener(MouseEvent.MOUSE_WHEEL, zoom);

private function zoom(mev:MouseEvent):void
{
    mev.delta > 0 ?
        bg_image.scaleX = bg_image.scaleY = Math.max(scaleFactor * bg_image.scaleX, minScale) :
        bg_image.scaleX = bg_image.scaleY = Math.min(1/scaleFactor * bg_image.scaleX, maxScale) ;   
}