开发者

Most Flash games don't resize properly in a browser window. How can I easily make my game do this?

开发者 https://www.devze.com 2023-02-28 14:08 出处:网络
Most flash-based browser games don\'t seem to resize properly when the user resizes their browser window (i.e. ctrl+mousewheel).

Most flash-based browser games don't seem to resize properly when the user resizes their browser window (i.e. ctrl+mousewheel).

Example of bad resizing: Boxhead The Zombie Wars. Please refrain from playing for a m开发者_开发技巧oment, lest you forget about my question.

Some (surprisingly very few) actually do resize properly. Example (at least in Chrome): D.N.8

Is there a simple or standard technique to accomplish proper resizing? How do you do it?


This is possible, for starters you need to ensure that the stage doesn't scale itself automatically when the player viewport is changed, and pick an alignment for where you stage will 'stick' as the viewport grows:

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

Once that is done, you have to listen to the stage's RESIZE event, so that when the player window/viewport is resized, you can check the new size and relayout your UI accordingly:

var resizeTimeout:uint;

stage.addEventListener(Event.RESIZE, onResize);
function onResize(e:Event=null):void 
{
    // Try setting a delay to workaround a weird bug when leaving fullscreen mode where the
    // dimensions aren't updated properly, even though a resize event is triggered..?
    if (resizeTimeout)
        clearTimeout(resizeTimeout);

    resizeTimeout = setTimeout(
        function ()
        {
            trace("resizing: " + stage.stageWidth+"x"+stage.stageHeight);
            // Re-layout from here
        },
        250);
}

As you can see I work around a bug here, which may not be neccesary in your case. Without the scaling mode turned off, the player will automatically resize your stage to fit the viewport, keeping your application oblivious to the changes. This can result in problems when the width/height ratio changes.

Anyway I hope I got most of this right, it's been a while since I had to deal with this, I just happened to have this bit of code lying around still from an old project.

0

精彩评论

暂无评论...
验证码 换一张
取 消