Jump to content

Convert mouse position to local position (relatively to the container


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


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

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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


  • Recently Browsing   0 members

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