Search code examples
actionscript-3fullscreenaddeventlistener

AS3: fullscreen mode issue


this is my code

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

I have two problems in this code that I can't solve.

First of all when I enter the FULL_SCREEN - mode the bcg.visible becomes true and when i exit FULL_SCREEN - mode by clicking on mybtn the bcg.visible becomes false, but when i exit by hitting escape it doesn't. How to make it work?

And second of all I want't to remove the eventlistener imgZoom when I exit the FULL_SCREEN - mode. But when I place the removeLEventlistener as option1 then turning back to NORMAL - mode doesn't work, and when I place the removeLEventlistener as option2 then the listener will not remove?

Can anyone help?


Solution

  • You can be notified of fullscreen enter/exit events by adding a listener to the Stage: stage.addEventListener(FullScreenEvent.FULL_SCREEN, ...)

    Here is a link to the Stage documentation: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#event:fullScreen

    For the imgZoom issue you need to move the definition outside the btnClick function. The imgZoom variable is local to the btnClick function so it gets redefined each time the function is called. This means the second time you click on the button imgZoom is undefined and you can never remove the previous event listener.

    Moving the function definition outside the btnClick handler will let you remove it again later.

    Here is everything together:

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