Sign in to follow this  
A.Kman

Updating phaser game bounds on resize

Recommended Posts

Hey all!

 

I'm trying to update my game every time the screen is resized or turned from portrait to landscape and visa versa.

What I'm doing right now is using window.onresize = this.resize(); but resize() is being called every frame.

 

Does anyone either know why this isn't working or how to do this better?

 

Thank you!!!

Share this post


Link to post
Share on other sites

@samme is right, you have to use scaleMode.RESIZE.  I found that in a web browser, sometimes the resize callback didn't always get fired on the very last bit of resizing/moving, so here is how I implemented resizing/realigning:

In my Boot state, set the scale mode for the game:

this.game.scale.scaleMode = Phaser.ScaleManager.RESIZE;

 

Then, in my other game states (levels), I set flags for whether things need to be realigned (or not), and in the resize callback, only set the flag, and I do all the actual resizing in update(), like this:

create() {
    this.game.scale.setResizeCallback(this.alignLevel, this);
    this.resized = false;
    this.resizedDoubleCheck = false;
}

alignLevel() {
    this.resized = true;
}

update() {
    if (this.resized || this.resizedDoubleCheck) {
        // do all the realigning of things here
    }

    // set the flags to false, but ensure one final
    // realignment even if the setResizeCallback didn't fire
    if (this.resized) {
        this.resized = false;
        this.resizedDoubleCheck = true;
    } else if (this.resizedDoubleCheck) {
        this.resizedDoubleCheck = false;
    }
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.