AS3:全屏模式问题

时间:2014-11-24 08:51:25

标签: actionscript-3 fullscreen addeventlistener

这是我的代码

function btnClick (e:MouseEvent):void
{
    if (stage.displayState == StageDisplayState.NORMAL)
    {
        stage.displayState = StageDisplayState.FULL_SCREEN; 
        bcgr.visible = true;
        function imgZoom (e:MouseEvent):void
        {
            //zooming - code not relevant to my issue
        }
        stage.addEventListener (MouseEvent.MOUSE_WHEEL, imgZoom);
    }
    else
    {
        //option1:  stage.removeEventListener (MouseEvent.MOUSE_WHEEL, imgZoom);
        stage.displayState = StageDisplayState.NORMAL;
        bcgr.visible = false;
        //option2:  stage.removeEventListener (MouseEvent.MOUSE_WHEEL, imgZoom);
    }
}
mybtn.addEventListener (MouseEvent.CLICK, btnClick);

我在这段代码中遇到两个无法解决的问题。

首先,当我输入FULL_SCREEN - modebcg.visible变为true,当我退出FULL_SCREEN - mode时,点击mybtn bcg.visible变为false,但是当我通过点击escape退出时它不会。如何使它工作?

第二,当我退出eventlistener imgZoom时,我不想删除FULL_SCREEN - mode。但是,当我将removeLEventlistener作为option1时,然后返回NORMAL - mode不起作用,当我将removeLEventlistener作为option2时,则listener 1}}不会删除?

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您可以通过向舞台添加监听器来通知全屏进入/退出事件:stage.addEventListener(FullScreenEvent.FULL_SCREEN, ...)

以下是舞台文档的链接:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#event:fullScreen

对于imgZoom问题,您需要将定义移到btnClick函数之外。 imgZoom变量是btnClick函数的本地变量,因此每次调用函数时都会重新定义它。这意味着第二次单击按钮imgZoomundefined时,您永远无法删除上一个事件监听器。

将函数定义移到btnClick处理程序之外将允许您稍后再删除它。

以下是一切:

function imgZoom (e:MouseEvent):void
{
    //zooming - code not relevant to my issue
}

function fullScreenListener (e:FullScreenEvent)
{
    bcgr.visible = e.fullScreen;
    if (e.fullScreen)
    {
        stage.addEventListener (MouseEvent.MOUSE_WHEEL, imgZoom);
    }
    else
    {
        stage.removeEventListener (MouseEvent.MOUSE_WHEEL, imgZoom);
    }
}

function btnClick (e:MouseEvent):void
{
    if (stage.displayState == StageDisplayState.NORMAL)
    {
        stage.displayState = StageDisplayState.FULL_SCREEN; 
    }
    else
    {
        stage.displayState = StageDisplayState.NORMAL;
    }
}

stage.addEventListener (FullScreenEvent.FULL_SCREEN, fullScreenListener);
mybtn.addEventListener (MouseEvent.CLICK, btnClick);