How do I identify the index value of the specific tile I click on?

0 favourites
  • 3 posts
From the Asset Store
Piano tiles
$9.99 USD
Template for a piano tiles game, fully documented in comments and video
  • Let's say I have a tilemap as my map at runtime with 3 columns and 3 rows. Trees, grass, whatever...doesn't matter.

    I want to click on 1 particular tile at runtime and identify which index value it is. Not which tile in the tilemap tiles it is but whether I clicked on tile 1 through 9 that is laid out at run time. Not the value of this:

    Also, this is not what I am after either (I mean it gets me really close but not close enough to what I want):

    Tilemap2.PositionToTileX(Mouse.X),Tilemap2.PositionToTileX(Mouse.Y) as this returns the column and row such as 1,2 meaning the second column, and third row based on the 0-index.

    What I want is to return 8. Because then I can have an array for the tiles and just feed data to match the tile. I guess another way to look at it is there a way to take that 1,2 example above and translate it to a single value returned? Right now I am having to use it as the image below but it seems wrong to run it this way. I would then have two arrays, one for the X and one for the Y and then have to evaluate them together. It seems inefficient but I'm still learning so I don't know. I feel like I am just missing some method to the object that converts that column and row nicely into a unique number.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • is there a way to take that 1,2 example above and translate it to a single value returned?

    With the width of the Tilemap you could use: y*width+x => y*3+x => 2*3+1 = 7 that is 0 indexed, you need to add 1 for your 8.

    y and x have to be 0 indexed and the width is the amount of tiles in X direction.

  • Thanks. I was thinking something around that direction or dividing the layout width by the tile size and evaluating where the X/Y mouse click is and running some evaluations that way.

    Also, I just took another look at that Demonoire example again and saw that they have tiles but also a sprite that is a destructible object. Maybe it's just better to do that method but instead of my game having X number of unique sprites, I just have a box that is not visible and it's used to store the data for each instance of the destructible I am targeting. I just worry about CPU use because one of my computers is real slow and one is pretty fast. Ah maybe I need to stop thinking of efficiency and just get on a move on it. But I feel like I am just giving up. :-?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)