I'm trying to make a 2D game where the level dynamically grows as the game progresses. I'm using TileMapLayer
s to build the tiles, and I want to have some sort of animation for the tiles as they spawn in, so they don't just appear.
I know you can create animated tiles for TileMapLayer
s using AnimatedTexture
s, but as far as I can see there isn't a way to trigger these effects through code (please correct me if I am wrong). What would be most helpful is if there is a way to bind an animation to autoplay on the set_cell
method of a TileMapLayer
, but I assume that isn't possible without source code modification? As that is most likely not possible, a way to play an animation on the tile in a custom function that calls set_cell
would be good. Alternatively, is it possible I might need to just use an AnimatedSprite
that covers the tile to play the animation in some way, then at the end of the animation place the tile?
Any help would be greatly appreciated.
Unfortunately, regular tiles in a tile map are relatively static affairs. Their animation capabilities are intended for looping animations, such as moving water, flames, rotating structures, etc.
I've used two methods to achieve single shot or triggered animations:
Using scene tiles, which allow instantiating a custom scene for a tile. This enables pretty much any behavior you want, for example starting a non-looping animation from the _enter_tree
callback of the instantiated scene, or triggering animations based on signals using the event bus design pattern. Do note, however, that this is much more inefficient than a regular atlas tile, so scene tiles should not be used to fill a large area of the tile map.
The option I typically use (and you mention it in your question) is to manually place a temporary scene to run whatever animation I need, then place or replace the atlas tile below it at the correct point in the animation. While it is a bit more work, I found that this gives a lot more flexibility (animation spanning multiple tiles, ability to fade the animation in and out, etc.). It also avoids the inefficiencies associated with scene tiles (assuming you remember to free the temporary scene when the animation completes...).
I suppose a combination of the 2 would also be possible, i.e., use a scene tile temporarily and then replace with an atlas tile when the animation completes. This would lose the flexibility of the additional "layer" achieved by the second option however.