Jump to content

Convert mouse position to local position (relatively to the container


no1no
 Share

Recommended Posts

Hi, I'm a Phaser beginner and I'm trying to make a chess-like game. I created a background sprite with a board image. The first thing I want to do is getting the coordination of position on the board. I get it by pointing the mouse to each position and printing this.game.input.x,y in update() but it seems this is the world coordination and I can't use it to set the piece position (using chessPiece.position.set). Is there anyway to convert the world position to the local position?

Link to comment
Share on other sites

Yes.

You're trying to map the game coordinates to your chess coordinates. It'll take some math. If you think about how your game is (for example) 800 pixels wide, and a chess board is 8 tiles wide, that means each tile is 800/8 === 100 pixels wide. So if the user clicks in pixel 234, that's 234/100 === 2nd tile.

Link to comment
Share on other sites

Thanks. But that's not really what I want. I could calculate the position using math but I figure I would use the function of converting the world position to local position in the future so I was asking anyway. For example in a RTS game I could spawn an unit at the mouse position on the board. I tried

this.game.world.toLocal(new PIXI.Point(this.game.input.x, this.game.input.y), parentObject)

but it's incorrect.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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