Cover.ts 1.98 KB
Newer Older
范雪寒's avatar
范雪寒 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class CoverClass extends cc.Component {

    // @property
    // url: string = '';
    // @property(cc.Node)
    // coverFrame: cc.SpriteFrame = null;
    coverNode: cc.Node = null;
    loaded = false;
    spriteFrameCache = {};

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.coverNode = this.node.getChildByName('cover')
    }

    start () {
        
        // this.coverFrame = this.coverNode.getComponent(cc.Sprite).spriteFrame
    }
    setSpriteFrame(spriteFrame) {
        if(!this.coverNode) {
            return;
        }
        if(!spriteFrame) {
            return;
        }
        const {width, height} = spriteFrame.getOriginalSize()
        this.coverNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        this.coverNode.width = width;
        this.coverNode.height = height;
        const sx = this.node.width / width;
        const sy = this.node.height / height;
        const s = Math.min(sx, sy);
        const scale = Math.round(s * 1000) / 1000;
        this.coverNode.scale = scale * .9
        return spriteFrame
    }
    setUrl(url) {
        if (!url) {
            return;
        }
        if (this.spriteFrameCache[url]) {
            this.setSpriteFrame(this.spriteFrameCache[url]);
            return;
        }
        if (this.coverNode) {
            cc.assetManager.loadRemote( url, (err, tex) => {
                const spriteFrame = new cc.SpriteFrame(tex);
                // console.log(spriteFrame);
                this.setSpriteFrame(spriteFrame);
                this.spriteFrameCache[url] = spriteFrame
            });
        }
       
       
    }

    // update (dt) {}
}