
import { _decorator, Node, Sprite, Animation, UITransform, resources, SpriteAtlas, SpriteFrame, AnimationClip} from 'cc';
const { ccclass, property } = _decorator;
 
@ccclass('RoleSprite')
export class RoleSprite extends Node {    

    constructor() {
        super();
        this.addComponent(Animation);
        let sprite = this.addComponent(Sprite);
        sprite.trim = true;
        sprite.sizeMode = Sprite.SizeMode.TRIMMED;
        
        const uiTrans = this.getComponent(UITransform)!;
        uiTrans.setContentSize(512, 512);
    }

    public onLoad(parent: Node, type: number = 0): void {
        let thisSelf = this;

        let start: string = "stand00";
        let aniLst: string[] = ["roles/test/stand"];        
        resources.load(aniLst, SpriteAtlas, function (error, assets: SpriteAtlas[]):void {
            if(error){
                console.log(error.message);
                return;
            }

            let spriteFrames: SpriteFrame[] = [];
            for(var i = 0; i < 10; i ++){
                let name = start + i.toString();
                let spriteFrame = assets[0].getSpriteFrame(name);
                spriteFrames.push(spriteFrame);
            }
            
            let animation = thisSelf.getComponent(Animation);
            let clip = AnimationClip.createWithSpriteFrames(spriteFrames, 18);            
            clip.wrapMode = AnimationClip.WrapMode.Loop;
            clip.name = "stand";
            animation.createState(clip);                   

            parent.addChild(thisSelf);
            animation.play("stand");
        });
    }
}