Thursday, May 8, 2014

Problem #2: Centering the game and make camera follow the player

I want my game to be on the center of the screen, so I asked my best friend (Google) on how to do that. Being able to find solutions through Google is a good skill to have.

Basically, I stumbled upon this thread: http://www.html5gamedevs.com/topic/1609-centering-canvas/

But it doesn't work. Basically after poking around in other threads and spending around an hour to do so, I realized that the code is wrong. You're supposed to put this code inside the create() function:

this.game.scale.pageAlignHorizontally = true;
this.game.scale.pageAlignVertically = true;
this.game.scale.refresh();

and voila, it's in the center.



(But it's not in the center when I put it in the web. Dammit, some more problems to solve.)

The next one is I want the camera to follow the player. Now I'm not sure why I seemed to be having a hard time yesterday, but actually it's just putting this code inside the function create():
player.anchor.setTo(0.5, 0.5);
game.camera.follow(player, Phaser.Camera.FOLLOW_PLATFORMER);

And you don't actually need the first line. See how easy it is to do stuff in Phaser? :D

No comments:

Post a Comment