How to change the image for a sprite?

In my case, i want to generate enemies by using one game objects and set the sprite and script during init callback. it is possible? because there are too many animations for enemies(in total about 150), it’s not very convenient to make go for all the enemies and make factory for them, so i decide to change the texture and behavior script in runtime. it’s possible?

for example in init callback something like this:
go.set("#sprite",“image”,{path = “main/graphic/enemy1.atlas”})

i know it can be done by set_texture in gui,so what about in go?

1 Like

In the atlas you can setup animations. You want to include all sprites in a single atlas in this case. You could also have multiple sprites on a go with a different atlas associated with each sprite, and then disable/enable which one based on unit.

To set a sprite’s animation you use

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

To disable/enable

msg.post("#sprite2", "disable")
msg.post("#sprite3", "enable")

There will probably be sprite.enable / sprite.disable / sprite.play in the future as aliases to these but they don’t exist yet…

8 Likes