How to add animated images in the game?
-
I want to add an animated gif/xcf image to my game. how would I do it?
I tried
var animatedText: Image = image(resourcesVfs["game_title.gif"].readBitmap()) { dockedTo(Anchor.TOP_CENTER, ScaleMode.EXACT ) }
but that doesnt works.
-
It is not straightforward. But you can read all the frames of the GIF and great a SpriteAnimation. Thought with a fixed
frameTime
even if the GIF has different frame times. You could also implement the animation image changing yourself manually. In the case is it enough for you to have a fixed frame time:import com.soywiz.korge.* import com.soywiz.korge.view.* import com.soywiz.korim.bitmap.* import com.soywiz.korim.color.* import com.soywiz.korim.format.* import com.soywiz.korio.file.std.* suspend fun main() = Korge(width = 512, height = 512, bgcolor = Colors["#2b2b2b"]) { val frames = resourcesVfs["ed3002fb3b703f38807ddb7da020169d.gif"].readBitmapImageData(GIF).frames val frameTime = frames.first().time val animation = SpriteAnimation(frames.map { it.bitmap.slice() }, frameTime) val sprite = sprite(animation) sprite.playAnimationLooped(animation) }