# Positioning problem

## Recommended Posts

Hi all I'm getting started with phaser and I was working on a candy crush like game, I found this really cool tutorial http://www.joshmorony.com/how-to-create-a-candy-crush-or-bejeweled-game-in-phaser/ and it all works great I even manage to add on some particle effects and I'm starting to play with screen sizes etc, not I have a huge problem, I have been looking at it for over a week and no luck, basically my problem es the placement of the tileGrid (see code below) the problem is that this is generated on the (0,0) point but I would like to move it a little bit down and maybe even be able to center it, is this something that is possible to do? my whole game is base on tutorial except for all the extra stuff that I'm adding and changing, please help.

`me.tileGrid = [`
`    ``[``null``, ``null``, ``null``, ``null``],`
`    ``[``null``, ``null``, ``null``, ``null``],`
`    ``[``null``, ``null``, ``null``, ``null``],`
`    ``[``null``, ``null``, ``null``, ``null``],`
`    ``[``null``, ``null``, ``null``, ``null``],`
`    ``[``null``, ``null``, ``null``, ``null``],`
`    ``[``null``, ``null``, ``null``, ``null``],`
`    ``[``null``, ``null``, ``null``, ``null``]`
`];`

##### Share on other sites

I have achieved this in the past by calculating the grid size, and subtracting it from your center point. So lets say you want the centre of your grid to sit at 300 x 300. You may use something like this. (a few presumptions have to be made in order to achieve this).

``````centerGrid(row, col, centerX, centerY) {
// row = 4;
// col = 5;
// centerX = 300;
// centerY = 300;

let gridWidth = 64;
let gridHeight = 64;
let gridSpace = 20;

let gridMaxWidth = (gridWidth + gridSpace) * row; //336
let gridMaxHeight = (gridHeight + gridSpace) * col; // 420

let gridCenterWidthX = gridMaxWidth / 2; // 168
let gridCenterHeightY = gridMaxHeight / 2; // 210

let gridCenterX = gridCenterWidthX + centerX; // 468
let gridCenterY = gridCenterHeightY + centerY; // 510

return {x: gridCenterX, y: gridCenterY};
}``````

You can make this much simpler however, by performing the calculations in one line like so:

``````centerGrid(row, col, centerX, centerY, gridWidth, gridHeight, gridSpace) {
let gridCenterX = (((gridWidth + gridSpace) * row) / 2) + centerX);
let gridCenterY = (((gridHeight + gridSpace) * col) / 2) + centerY);
return {x: gridCenterX, y: gridCenterY};
}``````

##### Share on other sites

Omg thanks so so much I will try tonight

##### Share on other sites

• 1 month later...

May I ask how do I apply the calculations to the Grid?

## 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.