Help needed in setting sprite at runtime from assets

Hi, I am trying to set load a sprite from assets and setting it to a sprite component by following code

var cardBg = this.app.assets.find(spriteTag + ".png", "texture");

console.log('Setting card ui for ' + cardBg.getFileUrl());

this.cardFront.sprite = cardBg;

I have correctly named the sprite assets and added tags to it.
But the above code is now working. Can anyone please help me out?
I also tried this.app.assets.fingByTag() - but this also doesnot work.

Hi @Arjun_Sankhala,

I think there are a number of things wrong there:

  1. You need to load a sprite asset, instead of a texture asset.

  2. Where are you trying to use that sprite? If this.cardFront is an entity and you want to use it to the element component you need to use that member property.

  3. cardBg is an asset, you need to assign its resource to the property.

Something like this, and make sure the asset is loaded (e.g. set preload to true):

var cardBg = this.app.assets.find("Sprite Name", "sprite");
this.cardFront.element.sprite = cardBg.resource;

Hope that helps.

Thanks @Leonidas
this worked for me
var cardFrontBG = this.app.assets.find(“name”, “sprite”);

this.cardFrontSprite.sprite.sprite = cardFrontBG.resource;
1 Like