# How can I change the animation of a tile?

**URL:** <https://forums.solar2d.com/t/how-can-i-change-the-animation-of-a-tile/324843>\
**Category:** Million Tile Engine\
**Created:** [April 23, 2014, 1:32am UTC](https://forums.solar2d.com/t/how-can-i-change-the-animation-of-a-tile/324843 "2014-04-23T01:32:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam.qwan](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@adam.qwan](https://forums.solar2d.com/u/adam.qwan)\
**Post date:** [April 23, 2014, 1:32am UTC](https://forums.solar2d.com/t/how-can-i-change-the-animation-of-a-tile/324843/1 "2014-04-23T01:32:19Z")

</div>

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

Thanks all!

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 23, 2014, 1:40pm UTC](https://forums.solar2d.com/t/how-can-i-change-the-animation-of-a-tile/324843/2 "2014-04-23T13:40:48Z")

</div>

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()
```

---

<div class="post-metadata">

**Author:** ![adam.qwan](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@adam.qwan](https://forums.solar2d.com/u/adam.qwan)\
**Post date:** [April 23, 2014, 7:07pm UTC](https://forums.solar2d.com/t/how-can-i-change-the-animation-of-a-tile/324843/3 "2014-04-23T19:07:35Z")

</div>

Great explanation and support! &nbsp;Thank you Dyson! &nbsp;

---

<div class="post-metadata">

**Author:** ![dyson122](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@dyson122](https://forums.solar2d.com/u/dyson122)\
**Post date:** [April 23, 2014, 1:40pm UTC](https://forums.solar2d.com/t/how-can-i-change-the-animation-of-a-tile/324843/4 "2014-04-23T13:40:48Z")

</div>

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()
```

---

<div class="post-metadata">

**Author:** ![adam.qwan](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@adam.qwan](https://forums.solar2d.com/u/adam.qwan)\
**Post date:** [April 23, 2014, 7:07pm UTC](https://forums.solar2d.com/t/how-can-i-change-the-animation-of-a-tile/324843/5 "2014-04-23T19:07:35Z")

</div>

Great explanation and support! &nbsp;Thank you Dyson! &nbsp;
