Flash AS3 - Child MovieClip上的StartDrag()中的StartDrag()

时间:2011-05-19 21:44:45

标签: flash actionscript-3 movieclip

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

如你所见,我有一个容器MC,我已添加到舞台上。我使用Rectangle()设置其拖动约束。然后我将'cat'子动画片段添加到容器中,我希望它也可以拖动。但是,一旦我在测试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();
}

3 个答案:

答案 0 :(得分:0)

我认为你必须测试当前目标 因为拖动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));
}

答案 1 :(得分:0)

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

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

要列出的内容有太多优点。

答案 2 :(得分:0)

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

同时尝试cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown, true);

相关问题