当mouseX = 0时,AS3鼠标事件未触发

时间:2013-04-02 19:17:03

标签: actionscript-3 flash mouseevent mousedown mouseup

我正在创建应该是Flash Develop中一个非常简单的全屏拖放游戏。除了一个令人沮丧的例子外,它的工作正常。

我向舞台添加项目,向它们添加MOUSE_DOWN侦听器,并在听到该侦听器时开始拖动。然后我添加一个MOUSE_UP监听器来确定何时停止拖动。同样,除非mouseX = 0,否则这样可以正常工作。当鼠标一直到屏幕左侧时,我鼠标向上或鼠标向下,没有触发器被触发。我也把它从全屏模式中取出,如果鼠标处于或低于0,则不会触发鼠标事件。

世界上到底发生了什么?

    private function itemSelectedHandler(e:MouseEvent):void 
    {
        thisItem = GameItem(e.currentTarget);
        thisItem.startDrag();
        stage.addEventListener(MouseEvent.MOUSE_UP, itemUnselectedHandler, false, 0, true);
    }

    private function itemUnselectedHandler(e:MouseEvent):void 
    {
        stopDrag();
        stage.removeEventListener(MouseEvent.MOUSE_UP, itemUnselectedHandler);
        thisItem.removeEventListener(MouseEvent.MOUSE_DOWN, itemSelectedHandler);   
    }

1 个答案:

答案 0 :(得分:3)

您正在类上调用stopDrag而不是拖动的精灵。尝试以下内容:

package 
{
    public class Main extends Sprite 
    {

        private var _draggedSprite:Sprite = null;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            for (var i:int = 0; i < 10; i++) 
            {
                createBox();
            }
        }

        private function createBox():void
        {
            var sp:Sprite = new Sprite();
            sp.graphics.beginFill(0xff0000, 1);
            sp.graphics.drawRect(0, 0, 30, 30);
            sp.graphics.endFill();
            sp.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);

            sp.x = Math.random() * (stage.stageWidth - 30);
            sp.y = Math.random() * (stage.stageHeight - 30);

            addChild(sp);
        }

        private function onMouseDown(e:MouseEvent):void 
        {
            var sp:Sprite = e.target as Sprite;
            sp.startDrag();
            _draggedSprite = sp;

            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        }

        private function onMouseUp(e:MouseEvent):void 
        {
            _draggedSprite.stopDrag();
            _draggedSprite = null;
            stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        }

    }

}

当全屏模式下mouseX = 0时,这对我有用。