How can I change the animation of a tile?

I would like to change the animation sequence for a tile that is animated and set in tiled properties.  Could someone offer a simple example for how I would change the tile animation of a tile?  I was looking at editing the animFrames, but I think i’m approaching this wrong.

Thanks all!

Hello Adam,

Tile animation sequenceData is determined when the map loads and loadMap cycles through the tileset data. Simply changing the animFrames won’t work because you aren’t also updating the pre-generated sequenceData. Of course, if you do change this data, you will be affecting all instances of that tile on the map. You can’t change the animation of one individual tile.

I threw together a function for performing the above steps;

local tile = mte.getTileAt({locX = 53, locY = 40, layer = 1}) print(tile) local changeTileAnimation = function(tileIndex, animDelay, animFrameSelect, animFrames, animSync) local map = mte.getMap() --Determine tileset index, tile frame index, and tile string local tilesetIndex = 1 for i = 1, #map.tilesets, 1 do if tileIndex \>= tonumber(map.tilesets[i].firstgid) then tilesetIndex = i else break end end local frameIndex = tileIndex - (map.tilesets[tilesetIndex].firstgid) local tileStr = tostring(frameIndex) map.tilesets[tilesetIndex].tileproperties[tileStr] = { animDelay = animDelay, animFrameSelect = animFrameSelect, animFrames = animFrames, animSync = 1 } local tempFrames = animFrames if map.tilesets[tilesetIndex].tileproperties[tileStr].animFrameSelect == "relative" then local frames = {} for i = 1, #tempFrames, 1 do frames[i] = (frameIndex + 1) + tempFrames[i] end map.tilesets[tilesetIndex].tileproperties[tileStr]["sequenceData"] = { name="null", frames=frames, time = tonumber(map.tilesets[tilesetIndex].tileproperties[tileStr]["animDelay"]), loopCount = 0 } elseif map.tilesets[tilesetIndex].tileproperties[tileStr].animFrameSelect == "absolute" then map.tilesets[tilesetIndex].tileproperties[tileStr]["sequenceData"] = { name="null", frames=tempFrames, time = tonumber(map.tilesets[tilesetIndex].tileproperties[tileStr]["animDelay"]), loopCount = 0 } end end changeTileAnimation(tile, 1000, "relative", {1, 2, 3, 4}, 1) --mte.updateTile({locX = 53, locY = 40, layer = 1}) mte.refresh()

Great explanation and support!  Thank you Dyson!  

Hello Adam,

Tile animation sequenceData is determined when the map loads and loadMap cycles through the tileset data. Simply changing the animFrames won’t work because you aren’t also updating the pre-generated sequenceData. Of course, if you do change this data, you will be affecting all instances of that tile on the map. You can’t change the animation of one individual tile.

I threw together a function for performing the above steps;

local tile = mte.getTileAt({locX = 53, locY = 40, layer = 1}) print(tile) local changeTileAnimation = function(tileIndex, animDelay, animFrameSelect, animFrames, animSync) local map = mte.getMap() --Determine tileset index, tile frame index, and tile string local tilesetIndex = 1 for i = 1, #map.tilesets, 1 do if tileIndex \>= tonumber(map.tilesets[i].firstgid) then tilesetIndex = i else break end end local frameIndex = tileIndex - (map.tilesets[tilesetIndex].firstgid) local tileStr = tostring(frameIndex) map.tilesets[tilesetIndex].tileproperties[tileStr] = { animDelay = animDelay, animFrameSelect = animFrameSelect, animFrames = animFrames, animSync = 1 } local tempFrames = animFrames if map.tilesets[tilesetIndex].tileproperties[tileStr].animFrameSelect == "relative" then local frames = {} for i = 1, #tempFrames, 1 do frames[i] = (frameIndex + 1) + tempFrames[i] end map.tilesets[tilesetIndex].tileproperties[tileStr]["sequenceData"] = { name="null", frames=frames, time = tonumber(map.tilesets[tilesetIndex].tileproperties[tileStr]["animDelay"]), loopCount = 0 } elseif map.tilesets[tilesetIndex].tileproperties[tileStr].animFrameSelect == "absolute" then map.tilesets[tilesetIndex].tileproperties[tileStr]["sequenceData"] = { name="null", frames=tempFrames, time = tonumber(map.tilesets[tilesetIndex].tileproperties[tileStr]["animDelay"]), loopCount = 0 } end end changeTileAnimation(tile, 1000, "relative", {1, 2, 3, 4}, 1) --mte.updateTile({locX = 53, locY = 40, layer = 1}) mte.refresh()

Great explanation and support!  Thank you Dyson!