如何在as3中像在flash中一样进行捕捉?

发布于 2025-01-08 16:10:05 字数 3084 浏览 1 评论 0原文

舞台上放置了很多电影片段?现在我想使用拖放到影片剪辑内来对齐它们。我希望将选定的影片剪辑放在该影片剪辑下方时出现垂直对齐线,如所附图像所示。

这是一个图像:

在此处输入图像描述

这是代码:

package  
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class Main extends MovieClip
    {
        private var obj:Rectangle1 = new Rectangle1();
        private var obj1:Rectangle1 = new Rectangle1();
        private var obj2:Rectangle1 = new Rectangle1();
        private var obj3:Rectangle1=new Rectangle1();
        public var Obj_Arr:Array = [];
        private var p:VerticalLine;

        public function Main() 
        {
            obj.x=20;
            obj.y=100;
            addChild(obj);
            obj1.buttonMode = true;
            trace("init()")
            obj.buttonMode=true;
            obj1.x=150;
            obj1.y=100;
            addChild(obj1);

            obj2.buttonMode=true;
            obj2.x=280;
            obj2.y=100;
            addChild(obj2);

            obj3.buttonMode=true;
            obj3.x=430;
            obj3.y=100;
            addChild(obj3);

            p = new VerticalLine();
            addChild(p);
            p.visible = false;

            Obj_Arr.push(obj);
            Obj_Arr.push(obj1);
            Obj_Arr.push(obj2);
            Obj_Arr.push(obj3);

            for (var i:int = 0; i < Obj_Arr.length; i++)
            {
                Obj_Arr[i].index = i;
                Obj_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, OnDown);
                Obj_Arr[i].addEventListener(MouseEvent.MOUSE_UP, OnUp);
            }
        }

        private function OnDown(e:MouseEvent):void
        {
            e.target.startDrag();
            trace(e.target.index);
            trace("remove...")
            Obj_Arr.splice(e.target.index,1)
            e.target.addEventListener(MouseEvent.MOUSE_MOVE, OnMove);
        }

        private function OnMove(e:MouseEvent):void
        {
            for (var i:int = 0; i < Obj_Arr.length; i++)
            {
                if(e.target.x==(Obj_Arr[i].x)) 
                {
                    p.visible = true;
                    p.x = Obj_Arr[i].x;
                    p.y = Obj_Arr[i].y;
                    i = Obj_Arr.length;
                }
                else
                {
                    p.visible =false;
                }
            }
        }

        private function OnUp(e:MouseEvent):void
        {
            p.visible =false;
            e.target.stopDrag();
            Obj_Arr.push(e.target);
            for (var i:int = 0; i < Obj_Arr.length; i++)
            {       
                if(e.target.index == (Obj_Arr[i].index)) 
                {
                    trace("add...");
                    Obj_Arr[i].x = e.target.x;
                    Obj_Arr[i].index = i;
                }
            }
            e.target.removeEventListener(MouseEvent.MOUSE_MOVE, OnMove);
        }
    }   
}

但此代码无法正常工作。

请帮忙!

提前致谢。

There are many movieclips placed on stage? Now I want to align them using drag and drop inside movieclips.I want the vertical alignment line appear while placing the selected movieclip under that movieclip as shown in the attached image.

Here is an image:

enter image description here

Here is the code:

package  
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class Main extends MovieClip
    {
        private var obj:Rectangle1 = new Rectangle1();
        private var obj1:Rectangle1 = new Rectangle1();
        private var obj2:Rectangle1 = new Rectangle1();
        private var obj3:Rectangle1=new Rectangle1();
        public var Obj_Arr:Array = [];
        private var p:VerticalLine;

        public function Main() 
        {
            obj.x=20;
            obj.y=100;
            addChild(obj);
            obj1.buttonMode = true;
            trace("init()")
            obj.buttonMode=true;
            obj1.x=150;
            obj1.y=100;
            addChild(obj1);

            obj2.buttonMode=true;
            obj2.x=280;
            obj2.y=100;
            addChild(obj2);

            obj3.buttonMode=true;
            obj3.x=430;
            obj3.y=100;
            addChild(obj3);

            p = new VerticalLine();
            addChild(p);
            p.visible = false;

            Obj_Arr.push(obj);
            Obj_Arr.push(obj1);
            Obj_Arr.push(obj2);
            Obj_Arr.push(obj3);

            for (var i:int = 0; i < Obj_Arr.length; i++)
            {
                Obj_Arr[i].index = i;
                Obj_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, OnDown);
                Obj_Arr[i].addEventListener(MouseEvent.MOUSE_UP, OnUp);
            }
        }

        private function OnDown(e:MouseEvent):void
        {
            e.target.startDrag();
            trace(e.target.index);
            trace("remove...")
            Obj_Arr.splice(e.target.index,1)
            e.target.addEventListener(MouseEvent.MOUSE_MOVE, OnMove);
        }

        private function OnMove(e:MouseEvent):void
        {
            for (var i:int = 0; i < Obj_Arr.length; i++)
            {
                if(e.target.x==(Obj_Arr[i].x)) 
                {
                    p.visible = true;
                    p.x = Obj_Arr[i].x;
                    p.y = Obj_Arr[i].y;
                    i = Obj_Arr.length;
                }
                else
                {
                    p.visible =false;
                }
            }
        }

        private function OnUp(e:MouseEvent):void
        {
            p.visible =false;
            e.target.stopDrag();
            Obj_Arr.push(e.target);
            for (var i:int = 0; i < Obj_Arr.length; i++)
            {       
                if(e.target.index == (Obj_Arr[i].index)) 
                {
                    trace("add...");
                    Obj_Arr[i].x = e.target.x;
                    Obj_Arr[i].index = i;
                }
            }
            e.target.removeEventListener(MouseEvent.MOUSE_MOVE, OnMove);
        }
    }   
}

But this code isnt working properly.

Please Help!

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮颜 2025-01-15 16:10:05

这是我想要的答案,我犯了一个错误,我在匹配条件内设置了数组索引,该索引应该在 MOUSE_UP 事件的 for 循环内设置。

package  
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class Main extends MovieClip
    {
        private var obj:Rectangle1 = new Rectangle1();
        private var obj1:Rectangle1 = new Rectangle1();
        private var obj2:Rectangle1 = new Rectangle1();
        private var obj3:Rectangle1=new Rectangle1();
        public var Obj_Arr:Array = [];
        private var vertical_line:VerticalLine;
        private var horizontal_line:HorizontalClass;

        public function Main() 
        {
            obj.x=20;
            obj.y=100;
            addChild(obj);
            obj1.buttonMode = true;

            obj.buttonMode=true;
            obj1.x=150;
            obj1.y=100;
            addChild(obj1);

            obj2.buttonMode=true;
            obj2.x=280;
            obj2.y=100;
            addChild(obj2);

            obj3.buttonMode=true;
            obj3.x=430;
            obj3.y=100;
            addChild(obj3);

            vertical_line = new VerticalLine();
            addChild(vertical_line);
            vertical_line.visible = false;

            horizontal_line = new HorizontalClass();
            addChild(horizontal_line);
            horizontal_line.visible = false;

            Obj_Arr.push(obj);
            Obj_Arr.push(obj1);
            Obj_Arr.push(obj2);
            Obj_Arr.push(obj3);

            for (var i:int = 0; i < Obj_Arr.length; i++)
            {
                Obj_Arr[i].index = i;
                Obj_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, OnDown);
                Obj_Arr[i].addEventListener(MouseEvent.MOUSE_UP, OnUp);
            }
        }

        private function OnDown(e:MouseEvent):void
        {
            e.target.startDrag();
            Obj_Arr.splice(e.target.index,1)
            e.target.addEventListener(MouseEvent.MOUSE_MOVE, OnMove);
        }

        private function OnMove(e:MouseEvent):void
        {
            for (var i:int = 0; i < Obj_Arr.length; i++)
            {
                if(e.target.x==(Obj_Arr[i].x)) 
                {
                    trace("vertical...");
                    vertical_line.visible = true;
                    vertical_line.x = Obj_Arr[i].x;
                    vertical_line.y = Obj_Arr[i].y;
                    i = Obj_Arr.length;
                }
                else
                {
                    vertical_line.visible =false;
                }               
            }

            for (var i:int = 0; i < Obj_Arr.length; i++)
            {
                if(e.target.y==(Obj_Arr[i].y)) 
                {
                    trace("horizontal...");
                    horizontal_line.visible = true;
                    horizontal_line.x = Obj_Arr[i].x;
                    horizontal_line.y = Obj_Arr[i].y;
                    i = Obj_Arr.length;
                }
                else
                {
                    horizontal_line.visible =false;
                }
            }
        }

        private function OnUp(e:MouseEvent):void
        {
            vertical_line.visible = false;
            horizontal_line.visible = false;
            e.target.stopDrag();
            Obj_Arr.push(e.target);

            for (var i:int = 0; i < Obj_Arr.length; i++)
            {       
                if(e.target.index == (Obj_Arr[i].index)) 
                {
                    Obj_Arr[i].x = e.target.x;                  
                }
                Obj_Arr[i].index = i;
            }
            e.target.removeEventListener(MouseEvent.MOUSE_MOVE, OnMove);
        }
    }   
}

Here is the answer what all i want,I was doing a mistake that i was setting the array index inside matching condition ,which should be set inside for loop on MOUSE_UP event.

package  
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class Main extends MovieClip
    {
        private var obj:Rectangle1 = new Rectangle1();
        private var obj1:Rectangle1 = new Rectangle1();
        private var obj2:Rectangle1 = new Rectangle1();
        private var obj3:Rectangle1=new Rectangle1();
        public var Obj_Arr:Array = [];
        private var vertical_line:VerticalLine;
        private var horizontal_line:HorizontalClass;

        public function Main() 
        {
            obj.x=20;
            obj.y=100;
            addChild(obj);
            obj1.buttonMode = true;

            obj.buttonMode=true;
            obj1.x=150;
            obj1.y=100;
            addChild(obj1);

            obj2.buttonMode=true;
            obj2.x=280;
            obj2.y=100;
            addChild(obj2);

            obj3.buttonMode=true;
            obj3.x=430;
            obj3.y=100;
            addChild(obj3);

            vertical_line = new VerticalLine();
            addChild(vertical_line);
            vertical_line.visible = false;

            horizontal_line = new HorizontalClass();
            addChild(horizontal_line);
            horizontal_line.visible = false;

            Obj_Arr.push(obj);
            Obj_Arr.push(obj1);
            Obj_Arr.push(obj2);
            Obj_Arr.push(obj3);

            for (var i:int = 0; i < Obj_Arr.length; i++)
            {
                Obj_Arr[i].index = i;
                Obj_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, OnDown);
                Obj_Arr[i].addEventListener(MouseEvent.MOUSE_UP, OnUp);
            }
        }

        private function OnDown(e:MouseEvent):void
        {
            e.target.startDrag();
            Obj_Arr.splice(e.target.index,1)
            e.target.addEventListener(MouseEvent.MOUSE_MOVE, OnMove);
        }

        private function OnMove(e:MouseEvent):void
        {
            for (var i:int = 0; i < Obj_Arr.length; i++)
            {
                if(e.target.x==(Obj_Arr[i].x)) 
                {
                    trace("vertical...");
                    vertical_line.visible = true;
                    vertical_line.x = Obj_Arr[i].x;
                    vertical_line.y = Obj_Arr[i].y;
                    i = Obj_Arr.length;
                }
                else
                {
                    vertical_line.visible =false;
                }               
            }

            for (var i:int = 0; i < Obj_Arr.length; i++)
            {
                if(e.target.y==(Obj_Arr[i].y)) 
                {
                    trace("horizontal...");
                    horizontal_line.visible = true;
                    horizontal_line.x = Obj_Arr[i].x;
                    horizontal_line.y = Obj_Arr[i].y;
                    i = Obj_Arr.length;
                }
                else
                {
                    horizontal_line.visible =false;
                }
            }
        }

        private function OnUp(e:MouseEvent):void
        {
            vertical_line.visible = false;
            horizontal_line.visible = false;
            e.target.stopDrag();
            Obj_Arr.push(e.target);

            for (var i:int = 0; i < Obj_Arr.length; i++)
            {       
                if(e.target.index == (Obj_Arr[i].index)) 
                {
                    Obj_Arr[i].x = e.target.x;                  
                }
                Obj_Arr[i].index = i;
            }
            e.target.removeEventListener(MouseEvent.MOUSE_MOVE, OnMove);
        }
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文