Deleting a tile via mouse click only works in the first time

ninjor1ninjor1 Posts: 1Member
edited December 2016 in 2D

so I'm working on a terraria clone (with the idea of having the ability to create vehicles and such).
Anyway, I'm able to click on a tile using :

if is_colliding():
    var hit_collider = get_collider()
    if hit_collider extends TileMap:
        var tilemap = hit_collider
        var hit_pos = get_collision_point()
        var tile_pos = tilemap.world_to_map(hit_pos)
Which works but when I delete the tile using:
tilemap.set_cellv(tile_pos, -1)

Subsequent clicks delete the wrong tiles (usually the top left first)
Almost like removing a tile messes up things and the tiles change order. Is there a way around this?


  • DmangamesDmangames Posts: 1Member

    I was also working on deleting tiles from the tile map for a miner game, but I didn't try to handle it through collision detection.
    I just use the mouse global position, and translate to a cell in the tilemap by dividing it by the cell size.

    var posx = get_global_mouse_pos().x
    var posy = get_global_mouse_pos().y
    var cellx = int(get_global_mouse_pos().x  / get_cell_size().x)
    var celly = int(get_global_mouse_pos().y / get_cell_size().y)

    Afterwards, you need to make some slight adjustments to account for negative positions so you remove the tile you expect
    if( sign(posx) == -1):
        cellx -= 1
    if( sign(posy) == -1):
        celly -= 1
Sign In or Register to comment.