Jump to content

Positioning problem


Rafaelx
 Share

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]
];

 

 

Link to comment
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};
}

 

Hope this answers your question :)

Link to comment
Share on other sites

  • 1 month later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...