[Solved] Changing the Resolution of FlxGame



  • I want to enable players to change the resolution of the game. Example: currently the game starts at 800x600 and I have a button that would change the resolution to 960x720. I wrote the below code:

    private function changeResolution(width:Int, height:Int):Void {
        FlxG.resizeWindow(width, height);
        FlxG.stage.removeChild(FlxG.game);
        FlxG.stage.addChild(new FlxGame(width, height, state.TitleState, 1, 30, 30, true, false));
    }
    

    I tested it and while it looks like it works, there are obviously things that will go wrong.
    That said, what is the best way to change the resolution on the fly. I don't mind if I have to "restart/re-init" the game.

    EDIT: Maybe I can pose the question this way:
    Is there a way to destroy the current game instance?



  • So I did more digging into scale modes and such and figured out a solution.

    I created a CustomScaleMode that extends FixedScaleMode and overrode the onMeasure function:

    override public function onMeasure(Width:Int, Height:Int):Void {
      FlxG.width = Width;
      FlxG.height = Height;
    
      updateGameSize(Width, Height);
      updateDeviceSize(Width, Height);
      updateScaleOffset();
      updateGamePosition();
    }
    

    Using this CustomScaleMode I was able to simply restart the state (ex. FlxG.switchState()) and the game was rendering at the new resolution.

    So far it works well, I hope this helps others with the same problem.

    EDIT: I also just realized that FlxG.resetGame() exists.


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.