cernec1999

Phaser Always Fix Camera Position to Sprite

Recommended Posts

I am developing a cross-platform overhead game using Phaser.

Is there a way to always fix the position of a camera to a sprite? The following function works great, but I want the camera to always be centered on the sprite - even when the sprite is nearing the edge of the tilemap.

this.game.camera.follow(this);

The reason I would like this capability is because I am developing a mobile game, and the player sprite manages to get in the way of crucial UI elements. This could be fixed by providing a constant camera on the sprite. Areas without tiles can be filled with a black background.

Here is my problem visualized. As you can see, when nearing the edge of the tilemap, the sprite is no longer centered and it is in the way of my joystick. Instead, I would like the sprite to be centered and the camera to display a black background past the tilemap when it gets near edges.

07UWi81.png

Thank you in advance,

Christopher

Share this post


Link to post
Share on other sites

The fix was quite simple. I simply set the world bounds using the function below. I also set the physics bounds so that the player will collide with the edge of the map.

    //set world bounds
    this.world.setBounds(this.boundsOffset * -1, this.boundsOffset * -1, this.map.widthInPixels + (this.boundsOffset * 2), this.map.heightInPixels + (this.boundsOffset * 2));

    //set physics bounds to be the scaleMode
    this.game.physics.arcade.setBounds(0, 0, this.map.widthInPixels, this.map.heightInPixels);

5989211a067d5_ScreenShot2017-08-07at10_24_56PM.thumb.png.799ae34c1f5200521b3d6c247a386c7b.png

Share this post


Link to post
Share on other sites
On 8/4/2017 at 11:29 PM, cernec1999 said:

I want the camera to always be centered on the sprite - even when the sprite is nearing the edge of the tilemap.

Simplest way is usually

game.camera.bounds = null;

 

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


  • Recently Browsing   0 members

    No registered users viewing this page.