stopImmediatePropagation对SimpleButton不起作用

时间:2016-05-17 17:44:11

标签: actionscript-3 mouseevent simplebutton

我试图取消此actionscript3代码中的事件:

public class Main extends Sprite
{
    public function Main()
    {

        this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    protected function onAddedToStage(event:Event):void
    {
        this.addEventListener(MouseEvent.MOUSE_OVER, onOver, true, int.MAX_VALUE);

        var btn:SimpleButton = new SimpleButton(
            createBtnState(0x0000FF), 
            createBtnState(0x00FFFF),
            createBtnState(0x00FF00),
            createBtnState(0x0000FF));
        btn.x = stage.stageWidth - btn.width >> 1;
        btn.y = stage.stageHeight - btn.height >> 1;
        addChild(btn);
    }
    private function createBtnState(color:uint):Sprite
    {
        var s:Sprite = new Sprite();
        s.graphics.beginFill(color, 1);
        s.graphics.drawRect(0,0,100,20);
        s.graphics.endFill();
        return s;
    }

    protected function onOver(event:MouseEvent):void
    {
        event.stopImmediatePropagation(); //Don't work
    }
}

如何取消事件悬停按钮? 在此示例中,按钮会在您悬停时响应。

1 个答案:

答案 0 :(得分:0)

您的悬停事件似乎已添加到主要按钮上。 尝试将事件监听器放在您声明的btn上。

protected function onAddedToStage(event:Event):void
{
    //this.addEventListener(MouseEvent.MOUSE_OVER, onOver, true, int.MAX_VALUE);

    var btn:SimpleButton = new SimpleButton(
        createBtnState(0x0000FF), 
        createBtnState(0x00FFFF),
        createBtnState(0x00FF00),
        createBtnState(0x0000FF));
    btn.x = stage.stageWidth - btn.width >> 1;
    btn.y = stage.stageHeight - btn.height >> 1;
    addChild(btn);

    btn.addEventListener(MouseEvent.MOUSE_OVER, onOver, true, int.MAX_VALUE);
}