Search code examples
actionscript-3air

Keep item at same monitor position regardless of window size/position


I am trying to create a game with fullscreen.

When I add an object to the stage in full screen mode, I would like it to stay at the same coordinates (for example 1000 pixels) relative to the monitor when I exit fullscreen mode.

How can I make the object move to the same location when leaving fullscreen mode?


Solution

  • To get your started:

    Something along these lines is what you'll need to do:

    stage.align = StageAlign.TOP_LEFT;  //you'll need to running a top-left no-scale swf for this to work
    stage.scaleMode = StageScaleMode.NO_SCALE;
    
    var itemPoint:Point = new Point(150,150);  //the point on the monitor the object should reside
    
    //call this anytime the item needs to be redrawn (eg when the window changes size or position)
    function updatePos(e:Event = null){
        //We need to also account for the chrome of the window 
        var windowMargin:Number = (stage.nativeWindow.bounds.width - stage.stageWidth) * .5; //this is the margin or padding that between the window and the content of the window
        var windowBarHeight:Number = stage.nativeWindow.bounds.height - stage.stageHeight - windowMargin; //we have to assume equal margin on the left/right/bottom of the window
    
        item.x = itemPoint.x - stage.nativeWindow.x - windowMargin;
        item.y = itemPoint.y - stage.nativeWindow.y - windowBarHeight;
    }
    
    stage.nativeWindow.addEventListener(NativeWindowBoundsEvent.MOVE, updatePos); //we need to listen for changes in the window position
    stage.nativeWindow.addEventListener(NativeWindowBoundsEvent.RESIZE, updatePos); //and changes in the window size
    
    //a click listener to test with
    stage.addEventListener(MouseEvent.CLICK, function(e:Event):void {
        if(stage.displayState == StageDisplayState.FULL_SCREEN_INTERACTIVE){
            stage.displayState = StageDisplayState.NORMAL;
        }else{
            stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
        }
    });
    
    updatePos();