1

我的变换工具显示的控件有问题。当我单击我的图像时,我得到了边界框(缩放或旋转图像),但是当我将鼠标悬停在角落上时,我没有得到光标来转换它。

带边界框的图像

我正在使用这些文件:

变换工具.as

变换工具控件.as

变换工具光标.as

这是我调用转换工具的代码:

var tool:TransformTool = new TransformTool();
    addChild(tool);

稍后会在单击图像时显示该工具,并在单击舞台时使该工具消失:

tmpImage.addEventListener(MouseEvent.CLICK, select);

function select(e:MouseEvent):void {
        tool.target = e.currentTarget as Sprite;
        stage.addEventListener(MouseEvent.MOUSE_DOWN, deselect);
    }

function deselect(e:MouseEvent):void {
        tool.target = null;
        tmpImage.addEventListener(MouseEvent.CLICK, select);
    }

我的边界框出现和消失的图像选择完美无缺。我所有的代码都按预期工作......除了边界框上的实际控件。请帮忙!

编辑

这个概念是用户可以从菜单中单击图像并将该图像的新实例拖到舞台上。然后用户可以单击新实例并且应该能够旋转或缩放它。然后他们可以单击图像以使边界框消失。(他们可以向舞台添加任意数量的图像)。

这是一些代码,显示了我实现的基本单击、创建新实例和拖动过程。

        //sb1 is the menu area that contains a group of images
        //hill is one of the images the user can add to the stage
        sb1.hill.addEventListener(MouseEvent.MOUSE_DOWN, createCopy);
            var i:int=0;
            var tmpImage:Sprite; //to store which image is being dragged currently

        function createCopy(e:MouseEvent):void {
            tmpImage = new Hill_mc();
            tmpImage.name = "hillChild"+(i++); //increment every copy
            container.addChild(tmpImage);
            tmpImage.x = mouseX-470;
            tmpImage.y = mouseY-270;
            tmpImage.startDrag();
            tmpImage.addEventListener(MouseEvent.MOUSE_DOWN, onDown); //add the mouse down to this new object
            stage.addEventListener(MouseEvent.MOUSE_UP, onUp); //since the mouse is currently down, we need to listen for mouse up to tell the current copy to stop dragging
        }

        //this will be called when click a copy
        function onDown(e:MouseEvent):void {
            tmpImage = Sprite(e.currentTarget); //get a reference to the one that was clicked, so we know which object to stop dragging on the global mouse up.
            container.addEventListener(MouseEvent.MOUSE_UP, onUp); //listen for the mouse up
            tmpImage.startDrag();
        }
        function onUp(e:MouseEvent):void {
            container.removeEventListener(MouseEvent.MOUSE_UP,onUp);
            if (tmpImage.hitTestObject(thesubmenu1)) {
                container.removeChild(tmpImage);
            }
            else {
                tmpImage.stopDrag();
            }
            tmpImage.addEventListener(MouseEvent.CLICK, select);
        }
        function select(e:MouseEvent):void {
            tool.target = e.currentTarget as Sprite;
            tmpImage.addEventListener(MouseEvent.MOUSE_DOWN, deselect);
        }

        function deselect(e:MouseEvent):void {
            tool.target = null;
            tmpImage.addEventListener(MouseEvent.CLICK, select);
        }

编辑

我找到了这段代码并将其放在我的 TransformTool.as 中。我觉得它是如此接近,并且必须有一些不正确的调用,因为我收到空对象引用的错误:

    public function select(event:Event):void {
        // the selected object will either be the
        // event target or current target. The current
        // target is checked first followed by target.
        // The parent of the target must match the
        // parent of the tool to be selected this way.

        if (event.currentTarget != this 
        && event.currentTarget.parent == parent){

            setTarget(event.currentTarget as DisplayObject, event);

        }else if (event.target != this 
        && event.target.parent == parent){

            setTarget(event.target as DisplayObject, event);

        }
    }

    /**
     * Helper selection handler for deselecting target objects. Set this
     * handler as the listener for an event that would cause the
     * deselection of a target object.
     * It is not required that you use this event handler. It is only a 
     * helper function that can optionally be used to help ease 
     * development.
     */
    public function deselect(event:Event):void {
        if (_target != null && event.eventPhase == EventPhase.AT_TARGET){
            setTarget(null, null);
        }
    }
4

1 回答 1

1

您提供的信息太少,无法确定究竟是什么问题。但是,有一个非常好的示例代码可以完全满足您的需求:

http://www.senocular.com/demo/TransformToolAS3/TransformTool.html (点击图片底部的链接。)

我相信你会成功的。

编辑 :

尝试使用内置的处理程序。我会做这样的事情:

而不是这个:

tmpImage.addEventListener(MouseEvent.CLICK, select);

function select(e:MouseEvent):void {
        tool.target = e.currentTarget as Sprite;
        stage.addEventListener(MouseEvent.MOUSE_DOWN, deselect);
    }

function deselect(e:MouseEvent):void {
        tool.target = null;
        tmpImage.addEventListener(MouseEvent.CLICK, select);
    }

做这个 :

tmpImage.addEventListener(MouseEvent.MOUSE_DOWN, tool.select);
stage.addEventListener(MouseEvent.MOUSE_DOWN, tool.deselect);

编辑 :

如果您没有处理程序,我不确定:) 但我建议在每个方法中删除事件侦听器,因为它们可能会相互干扰。

tmpImage.addEventListener(MouseEvent.CLICK, select);

function select(e:MouseEvent):void {
        tmpImage.removeEventListener(MouseEvent.CLICK, select);
        tool.target = e.currentTarget as Sprite;
        stage.addEventListener(MouseEvent.MOUSE_DOWN, deselect);
    }

function deselect(e:MouseEvent):void {
        stage.removeEventListener(MouseEvent.MOUSE_DOWN, deselect);
        tool.target = null;
        tmpImage.addEventListener(MouseEvent.CLICK, select);
    }
于 2015-02-27T09:16:43.640 回答