Changing sprite

How can i change the sprite of a game object in runtime?

You can either use animations and change the animation of the sprite or have multiple sprites on a game object and enable/disable them to switch between them

As @Johan first solution, the code is

msg.post("#sprite", "play_animation", {id = hash("another_image")})

Noted that “another_image” have to be in the same atlas of your original image.

Note that animations can be single frame images. Any images that you add to an atlas will be present as single frame animations with same name as the image file.

1 Like

I was using the play_animation but i thought that there is a simple way :grin:

Thanks for the replies ^^

Hi, is there any difference between “play_animation” message and sprite.flip_book function?
From reading the docs they look the same.

msg.post("#sprite", "play_animation", {id = hash("another_image")})

vs

sprite.play_flipbook("#sprite", "another_image" )

1 Like

No difference. The sprite.play_flipbook() function has the advantage of getting autocomplete in the code editor.

4 Likes
local screenWidth = sys.get_config(display.width)
local screenHeight = sys.get_config(display.height)
–print(screenWidth = , screenWidth , screenHeight = , screenHeight )
–go.set(#sprite, animation, self.my_atlas)
–local animation = go.get(#sprite, animation)
–print(animation = , animation)
go.set(#sprite, size, vmath.vector3(screenWidth,screenHeight,0))
go.set(., position, vmath.vector3(0.5screenWidth,0.5screenHeight , -1 ))
msg.post(#sprite, play_animation , {id = hash(bg)})

Could you phrase what you are trying to do? This is an old thread as well, if you have a question would be better to make it in a new thread.

3 Likes