//左边游戏列表中游戏图标预制体脚本

cc.Class({
    extends: cc.Component,

    properties: {
        icon: cc.Sprite,  //游戏图标
        labName: cc.Label,  //游戏名
        hongdian: cc.Node,  //小红点
        _appId:null,
        _path:null,
        _data: null,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
    },

    init: function(data){
        this._data =data;
        //显示图片icon
        this.showSpHead(this.icon.node,data.iconImageUrl);
        this.labName.string =data.name;  //游戏名字
        this._appId = data.appId;
        this._path = data.path;    
    },

    //显示图片
    showSpHead: function(node,avatarUrl){
        if (CC_WECHATGAME) {
            try {
                let image = wx.createImage();
                image.onload = () => {
                    try {
                        let texture = new cc.Texture2D();
                        texture.initWithElement(image);
                        texture.handleLoadedTexture();
                        node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
                    } catch (e) {
                        cc.log(e);
                        node.active = false;
                    }
                };
                image.src = avatarUrl;
            }catch (e) {
                cc.log(e);
                node.active = false;
            }
        } else {
            cc.loader.load({
                url: avatarUrl, type: 'jpg'
            }, (err, texture) => {
                node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
            });
        }
    },

    //跳转游戏
    onPlayGame(){
        console.log("跳转",this._appId,this._path);
        //点击后就隐藏小红点
        this.hongdian.active =false;
        cc.HQ.GameMgr.onPlayingOtherGame(this._appId,this._path);
        cc.game.emit("hongdian",1);
        //埋点
        sdk.setSceneEvent("热门推荐","点击",this._data.name,{},null);
    },

    // update (dt) {},
});
