Discord
Login
Community
DARK THEME

How to detect on what tile you are on a map?

I need this to take damage in my game :)

You could use the collision lib by microstudio:

if mapHit(player.x, player.y, "your_map") == "spikes" then
  player.hp -= 5
end

mapHit returns the name that the tile is touching the player, 0 if no. Although now that i notice, it will check if the center of the sprite is touching the spikes, for that, you should subtract to player.y the height of the sprite, like this:

mapHit(player.x, player.y-player.height, "your_map")

Yes, Maphit works well, but I also created a function if you want

Detecteur = function( x, y, map )
  if map.type == "string" then map = maps[map] end
  
  if not map then return 0 end
  if not map.screen_position then return 0 end
  local pos = map.screen_position
  local mx = pos.x - pos.w / 2
  local my = pos.y - pos.h / 2
  local dx = floor((x - mx) / pos.w * map.width)
  local dy = floor((y - my) / pos.h * map.height)
  if dx >= 0 and dx < map.width and dy >= 0 and dy < map.height then
    return map.get(dx, dy)
  else
    return 0
  end
end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community