Hello! So, I am creating a game, and I've been trying to animate water with STI and Tiled, I tried many ways to do so and the closest I've ever gotten (I guess) is with the setLayerTile function:
function map:update(dt)
self.waterTimer = self.waterTimer + dt
if self.waterTimer >= self.waterDuration then
self.waterTimer = 0
-- animate water tiles
for i, tile in pairs(self.waterTiles) do
if tile.gid == self.waterLastGID then
local gid = self.waterFirstGID
else
local gid = tile.gid + 1
end
local x, y = self.currentMap:convertPixelToTile(self:getTilePixelPosition(tile))
print(x, y)
self.currentMap:setLayerTile("water", x, y, gid)
end
end
end
function map:getTilePixelPosition(tile)
local x = self.currentMap.tilewidth * (tile.gid)
local y = self.currentMap.tileheight * math.floor((tile.gid) / self.currentMap.tilesets[5].columns)
return x, y
end
And the tiles never actually get changed. What am I doing wrong?
I did try, but it wouldn't make it possible to swap between each frame multiple times. I am now trying with instance.batch:set(), because a person said it worked for them.
function map:update(dt)
self.waterTimer = self.waterTimer + dt
if self.waterTimer >= self.waterDuration then
self.waterTimer = 0
-- animate water tiles
for i, tile in pairs(self.waterTiles) do
local gid = nil
if tile.gid == self.waterLastGID then
gid = self.waterFirstGID
else
gid = tile.gid + 1
end
local x, y = self.currentMap:convertPixelToTile(tile.x, tile.y)
local tileData = self.currentMap.layers["water"].data[y+1][x+1]
local tileInstance = self:getTileInstance(tileData)
if tileInstance then
local newTile = self.currentMap.tiles[gid]
self.waterTiles[i] = newTile
tileInstance.batch:set(tileInstance.id, newTile.quad, tileInstance.x, tileInstance.y)
end
end
end
end
function map:getTileInstance(tile)
local tileInstance = false
local px = tile.x
local py = tile.y
for _, ti in pairs(self.currentMap.tileInstances[tile.gid]) do
if ti.x == px and ti.y == py then
tileInstance = ti
break
end
end
return tileInstance
end
Unfortunately, the water is not changed at all still. I don't know what's wrong.