xzereus

Detecting click on a tile

Recommended Posts

I'm working on a basic little JRPG just to learn more about Phaser. I've got arrow controls working, but I'm trying to experiment with point and click. I've created a function that takes in a point (x, y) and moves the character to that point.

 

All I need to do is find the TILE'S x and y coordinates when that particular tile is clicked. All of the tiles I need to handle this for are on a single layer. I've tried adding an onInputDown event to the individual tiles and the layers, but it doesn't work (tiles don't have events, layers don't have onInputDown event). What's the best way to do something like this?

 

Thanks!

Share this post


Link to post
Share on other sites

Use a generic mouse down call, and calculate the position:

clickCallback = function(pointer){    //Tiles world coordinates:    var tileworldX = pointer.worldX - (pointer.worldX%tileWidth);    var tileworldY = pointer.worldY - (pointer.worldY%tileHeight);    //click at 100, 200 world give you 64, 192    //Tiles coordinates in the grid coords (eg 4 tiles across, 7 down):    var tileX = pointer.worldX / tileWidth;    var tileY = pointer.worldY / tileHeight;    //click at 100, 200 world give you 1, 3}

Share this post


Link to post
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.

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

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.