ActionScript 3:缩放功能 - 拖动不显示

时间:2014-02-10 12:03:34

标签: actionscript-3 drag-and-drop zoom movieclip

我提供了类似于本网站解释的缩放功能:http://www.flashandmath.com/howtos/zoom/

即使它在我的舞台上移动图像的可能性确实有效,但拖动动画不存在,这意味着我的图像不会“移动”,只会突然改变位置。 / p>

我试图将概念从基于时间轴的改为基于类。

这是我的主要课程:

package 
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Mouse;

public class Main extends Sprite
{
    public static var scale:Number = 1;
    private var _rootMC:MovieClip;

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

    public var bg_image:Sprite;
    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 static var scaleFactor:Number = 0.8;
    public static var minScale:Number = 0.25;
    public static var maxScale:Number = 10.0;
    //-------------------------------

    public function Main(rootMC:MovieClip)
    {
        _rootMC = rootMC;



        bg_image = new image();

        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);
        _rootMC.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

        spImage.addEventListener(MouseEvent.CLICK, zoom);

        _rootMC.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
        _rootMC.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)
        {
            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();
        }
    }

}
}

这是我的图像类:

package  {
import fl.motion.MatrixTransformer;
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.MouseEvent;
public class image extends MovieClip
    {

    public function image() 
    {
        this.width = 980
        this.height = 500
        this.y = 0
        this.x = 0
    }

}

我知道这是很多代码,但我真的被卡住了:(

1 个答案:

答案 0 :(得分:0)

在您的MOUSE_DOWN处理程序中,您需要开始监听MOUSE_MOVE事件。 在您的MOUSE_UP处理程序中,您需要停止监听MOUSE_MOVE事件。

不确定你是否已经这样做了。

在MOUSE_MOVE事件处理程序中,您需要更新图像的x / y位置,方法与您在MOUSE_UP处理程序上执行的操作类似。