Change sprite during runtime

I’m trying to make a mute toggle ui element in my game that toggles between two images, mute-true and mute-false. The UI Element > Image Element wasn’t working, so I went with adding a sprite element to a button. Each attempt to programmatically change the sprite at or during runtime has failed.

As far as my setup: should I have 1 sprite with 2 frames and try and change frames, or instead 2 sprites and the sprites get changed out?





Either will work. If you have 1 sprite asset with an animation of 2 frames, you can change the frame number on the element component

https://api.playcanvas.com/classes/Engine.ElementComponent.html#spriteFrame

If you are using 2 sprite assets, you need to have a reference to both assets in the script and assign which one you want to the sprite asset in the element component https://api.playcanvas.com/classes/Engine.ElementComponent.html#spriteAsset

Change sprite during runtime - still not finding success. I’m trying the route of changing sprites, and I think that I’ve got the two sprites toggled for preloading, but no matter what I try, I can’t get the mute button sprite to change. I will probably try changing over to the changing frames method tomorrow or soon. Regardless, thanks for the help

Got it working! :sweat_smile: Same url as above. Still can’t get cursor:pointer working on it though

Ah. I forgot about that bug with needing to use the asset id and not the asset object for element components :person_facepalming:

Good that you found it

1 Like

unfortunately, it was chatgpt that found it :sweat_smile: Enough spamming it with errors and questions and I got there. Now that it’s working, I do want to go back and figure out what I was doing that was making it so difficult. I need to better learn how to manage touch and mouse interactions globally in an app

There is a bug in the engine where the API is meant to take an asset object.

1 Like