# how does setBounds work?

## Recommended Posts

I've read the documentation that says:

Updates the size of this world and sets World.x/y to the given values The Camera bounds and Physics bounds (if set) are also updated to match the new World bounds.

So for example if I have a 320 x 200 canvas and want to extend that to 480 x 380 (random numbers here) I would do:

`game.world.setBounds(0,0,480,380);`

BUT would that yield this result

or this result

Would I have to give negative margins if it presently outputs result 1?

Thanks

##### Share on other sites

Hello,

you can add a simple graphics to your world with extended bounds to see where (0, 0) point is, that should tell you what you are looking for, or not?

Did I understand your question correctly?

##### Share on other sites

hmm yes, I did so and result is number 1.

So how would I go about centering?

I tried negative values but it does not seem to work

It seems I have to use negative values (now working) and then reposition the camera...

##### Share on other sites

I still can't understand centering.
If I add let's say 100px on the left and right of the screen like this:

`var game = this.game;    bound_margin = 100;    newLeft = this.game.world.bounds.left-bound_margin;    newRight = this.game.world.bounds.right+bound_margin;    game.world.setBounds(newLeft, 0, newRight, window.innerHeight);    // THIS MUGHT BE WRONG    bg = game.add.tileSprite(newLeft, 0, newRight, window.innerHeight, 'background');`

and then add some camera panning like so:

`update: function() {    if (cursors.left.isDown) {        this.game.camera.x -= bound_margin;    }    if (cursors.right.isDown) {        this.game.camera.x += bound_margin;    }}`

the camera only moves to one side!

Edit:-----------------------------------------------

Also what I don't understand is why even with the new bounds in place the ship drawing point still remains at the top left of the window.

It should be out of the window?

Edited by grumpygamer
##### Share on other sites

Ok, I've just understood by myself.

There is no particular need to set negative boundaries UNLESS you want to then calculate a 0-based area.

You only need to create the new bounds starting at 0,0, make them as big as you like, THEN center the camera.

Job done.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.