[SOLVED] How would I change or attach a sprite from the registry to a sprite element


#1

Basically I would like to find a sprite called “sword” something like this >>

this.app.assets.find("sword", "sprite")

Then attach the sprite to a blank sprite element. Either as a simple sprite, or animated sprite. This also is true for image elements. Something like this >>

this.entity.sprite.addClip(this.swordSprite)

or

this.entity.sprite.attach(this.swordSprite)

Obviously the above code doesn’t work, but I gave it a shot anyway lol.

I am not sure how I’d go about doing so any help would be great!


#2

Figured it out. Cheers anyways ;D

Here it is for future use.

//Setting up the different states of the sprites whether there should be a sword or not.
//By finding them using asset.find, and finding the (name, asset type)
    this.swordSprite = this.app.assets.find('sword','sprite');
    this.noSword = this.app.assets.find('noSword','sprite');

//Check if the sword is equipped or not, if it is use the sprite animation with a sword in it, versus the no sword animation. 
    if(this.sword.equipped){
        
        console.log('sword equipped');
        
        this.entity.sprite.clip('sword').spriteAsset = this.swordSprite.id; //You need the asset's id to use it directly. 
        this.entity.sprite.play('sword'); //Must play the animation otherwise it's attached without being played. 
        
    } else {
        
        console.log('no sword equipped');
        
        this.entity.sprite.clip('sword').spriteAsset = this.noSword.id;
        this.entity.sprite.play('sword'); 
        
    }