How to spawn a sprite/animated sprite using code?

@oxters168 Here is the way to do it without using the asset directly: https://playcanvas.com/editor/scene/1163586

    var animSpriteEntity = new pc.Entity(); // Create an Entity
    animSpriteEntity.addComponent("sprite", { 
        type: pc.SPRITETYPE_ANIMATED, autoPlayClip: 'clip1' 
    });
    
    var addedClip = animSpriteEntity.sprite.addClip({
        name: 'clip1',
        fps: 4,
        loop: true
    });
    
    addedClip.sprite = this.spriteAsset.resource;
    animSpriteEntity.sprite.play("clip1");
    
    this.entity.addChild(animSpriteEntity);

Also, see: How to create an Animation Sprite Clip with programmatically created Sprite?

2 Likes