Flash AS3 - 子 MovieClip 上的 StartDrag() 内的 StartDrag()

发布于 2024-11-08 17:18:42 字数 1217 浏览 0 评论 0原文

嘿大家, 我的代码列在下面。

正如你所看到的,我有一个容器 MC,我已将其添加到舞台上。我使用 Rectangle() 设置其拖动约束。然后,我将“猫”子动画剪辑添加到容器中,并且我希望它也可以拖动。然而,当我在测试MC时点击我的猫。它射向舞台上的 x=0 y=0 点并且不动。

容器MC可以毫无困难地移动。

如果我从容器 startdrag() 函数中删除矩形边界。两个 MC 都可以毫无问题地拖动。

任何帮助都会很棒。

谢谢

//panning ability
my_x = 800 - myImage.width;
my_y = 480 - myImage.height;

myWidth = 0 - my_x;
myHeight = 0 - my_y;

container.addEventListener(MouseEvent.MOUSE_DOWN, bgMouseDown);
container.addEventListener(MouseEvent.MOUSE_UP, bgMouseUp);

 function bgMouseDown(evt:MouseEvent):void
{
    var object = evt.currentTarget;
    object.startDrag(false, new Rectangle(my_x, my_y, myWidth ,myHeight));
}

 function bgMouseUp(evt:MouseEvent):void
{
    var object = evt.currentTarget;
    object.stopDrag();
}

//adding ze cat

cat = new ACat();
container.addChild(cat);
cat.x = 100;
cat.y = 400;


cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown);
cat.addEventListener(MouseEvent.MOUSE_UP, catMouseUp);


 function catMouseDown(evt:MouseEvent):void
{
    var object = evt.currentTarget;
    object.startDrag(false);
}

 function catMouseUp(evt:MouseEvent):void
{
    var object = evt.currentTarget;
    object.stopDrag();
}

hey everyone,
my code is listed below.

as you can see, I have a container MC which I have added to the stage. I set its drag constraints using a Rectangle(). I then add the 'cat' child movieclip to the container, and I want this to be dragable too. However, as soon as I click on my cat when testing the MC. It shoots to point x=0 y=0 on the stage and doesn't move.

The container MC can be moved without any trouble.

If I remove the rectangle bounds from the containers startdrag() function. both MC's can be dragged without any issue.

any help would be awesome.

thanks

//panning ability
my_x = 800 - myImage.width;
my_y = 480 - myImage.height;

myWidth = 0 - my_x;
myHeight = 0 - my_y;

container.addEventListener(MouseEvent.MOUSE_DOWN, bgMouseDown);
container.addEventListener(MouseEvent.MOUSE_UP, bgMouseUp);

 function bgMouseDown(evt:MouseEvent):void
{
    var object = evt.currentTarget;
    object.startDrag(false, new Rectangle(my_x, my_y, myWidth ,myHeight));
}

 function bgMouseUp(evt:MouseEvent):void
{
    var object = evt.currentTarget;
    object.stopDrag();
}

//adding ze cat

cat = new ACat();
container.addChild(cat);
cat.x = 100;
cat.y = 400;


cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown);
cat.addEventListener(MouseEvent.MOUSE_UP, catMouseUp);


 function catMouseDown(evt:MouseEvent):void
{
    var object = evt.currentTarget;
    object.startDrag(false);
}

 function catMouseUp(evt:MouseEvent):void
{
    var object = evt.currentTarget;
    object.stopDrag();
}

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

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

发布评论

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

评论(3

烟若柳尘 2024-11-15 17:18:42

在 catMouseDown 函数的第一行尝试 evt.stopPropagation( )

还可以尝试cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown, true);

Try evt.stopPropagation( ) on the first line of the catMouseDown function.

Also try cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown, true);

才能让你更想念 2024-11-15 17:18:42

我认为你必须测试 currentTarget
因为当您拖动 Cat mc 时会为容器触发该事件,

请尝试类似的操作:

function bgMouseDown(evt:MouseEvent):void
{
    var object = evt.currentTarget;
    if(evt.currentTarget !=container) return;
    object.startDrag(false, new Rectangle(my_x, my_y, myWidth ,myHeight));
}

I think you have to test wich is the currentTarget
because the event is fired for the container when you drag the Cat mc

try something like that :

function bgMouseDown(evt:MouseEvent):void
{
    var object = evt.currentTarget;
    if(evt.currentTarget !=container) return;
    object.startDrag(false, new Rectangle(my_x, my_y, myWidth ,myHeight));
}
﹏半生如梦愿梦如真 2024-11-15 17:18:42

从容器中删除侦听器,并为可拖动对象创建一个基类。

package
{
    import flash.display.Sprite;
    import flash.geom.Rectangle;
    import flash.events.MouseEvent;

    public class Dragable extends Sprite
    {
        // vars
        private var _rect:Rectangle;

        /**
         * Constructor
         */
        public function Dragable()
        {
            addEventListener(MouseEvent.MOUSE_DOWN, _drag);
            addEventListener(MouseEvent.MOUSE_UP, _drop);
        }

        /**
         * MOUSE_DOWN
         */
        private function _drag(e:MouseEvent):void
        {
            if(_rect != null) startDrag(false, _rect);
            else startDrag();
        }

        /**
         * MOUSE_UP
         */
        private function _drop(e:MouseEvent):void
        {
            stopDrag();
        }

        /**
         * Define a boundary Rectangle
         * @param rect The Rectangle to define
         */
        public function set boundaries(rect:Rectangle):void
        {
            _rect = rect;
        }
    }
}

这样做的优点太多了,无法一一列举。

Remove your listeners from the container, and make a base class for your dragable objects.

package
{
    import flash.display.Sprite;
    import flash.geom.Rectangle;
    import flash.events.MouseEvent;

    public class Dragable extends Sprite
    {
        // vars
        private var _rect:Rectangle;

        /**
         * Constructor
         */
        public function Dragable()
        {
            addEventListener(MouseEvent.MOUSE_DOWN, _drag);
            addEventListener(MouseEvent.MOUSE_UP, _drop);
        }

        /**
         * MOUSE_DOWN
         */
        private function _drag(e:MouseEvent):void
        {
            if(_rect != null) startDrag(false, _rect);
            else startDrag();
        }

        /**
         * MOUSE_UP
         */
        private function _drop(e:MouseEvent):void
        {
            stopDrag();
        }

        /**
         * Define a boundary Rectangle
         * @param rect The Rectangle to define
         */
        public function set boundaries(rect:Rectangle):void
        {
            _rect = rect;
        }
    }
}

There are too many advantages to this to list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文