// Learn cc.Class: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html // Learn Attribute: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html var PushAdBtn = cc.Class({ extends: cc.Component, properties: { icon: { default: null, type: cc.Sprite }, tempIcon: { default: null, type: cc.Sprite }, pushType: "", isLoop: true, loopTime: 10, actionTime: 0.4, pushDataType: "iconList", placement: "home", }, onLoad() { this.initData(); this.initListener(); cc.log("onLoad~~~~~~~~~~"); }, start() { this.node.active = false; this.pushCall(); cc.log("start~~~~~~~~~~"); this.initLoop(); }, check(pushData) { if (!pushData || pushData.length <= 0) { this.node.active = false; return false; } return true; }, initLoop() { if (!this.loopTime || this.loopTime <= 0) return; this.schedule(function () { this.loopNext(); }.bind(this), this.loopTime); }, initData() { this.data = null; this.dataIndex = -1; this.pushData = null; this.animation = this.icon.getComponent(cc.Animation); this.tempAnimation = this.tempIcon.getComponent(cc.Animation); this.tempIconPX = this.tempIcon.node.x; }, initListener() { cc.director.on('wxPushData', function (event) { cc.log("wxPushData~~~~~~~~~~~"); this.pushCall(); }.bind(this)); }, pushCall(){ if(!wb.wxData.pushData) return; if(this.havePushCall) return; this.havePushCall = true; this.pushData = wb.wxData.pushData[this.pushDataType]; var index = this.getDataIndex(); cc.log("pushCall " + index); // cc.log(this.pushData); this.data = this.pushData[index]; this.setData(this.data); }, getDataIndex() { return 0; }, setDataByIndex(index) { this.setData(this.pushData[index]); }, setData(data) { if (!data) return; this.data = data; this.createBtnSp(); this.showPost(); }, createBtnSp() { if (!this.data) return; cc.log("createBtnSp1 this.data " + this.data.icon); cc.loader.load({ url: this.data.icon }, function (err, tex) { cc.log("createBtnSp2 this.data " + this.data.icon); if (tex) { this.anName = null; this.node.active = true; this.tex = tex; if (this.data.plist && this.data.plist.length > 0) { wb.PlistHelper.parsePlist(this.data.plist, function (data) { this.dealPlist(data); }.bind(this)); } else { this.runTempIconAction(false); } } }.bind(this)); }, flushNameLabel(){ }, runTempIconAction(isAn) { var spf = null; // this.tempIcon.active = true; if (isAn) { if (!this.anName) return; //this.animation.play(this.anName); this.tempAnimation.play(this.anName); } else { //this.animation.stop(); this.tempAnimation.stop(); spf = new cc.SpriteFrame(this.tex); // this.icon.spriteFrame = spf; this.tempIcon.spriteFrame = spf; } this.isRunTempAn = true; var runTime = this.actionTime; if (!this.isFirst) { runTime = 0; this.isFirst = true; } var seq = cc.sequence(cc.moveTo(runTime, cc.v2(0, 0)), cc.callFunc(function () { this.isRunTempAn = false; if (isAn) { this.animation.play(this.anName); } else { this.animation.stop(); this.icon.spriteFrame = spf; } this.tempIcon.node.x = this.tempIconPX; // this.tempIcon.active = false; }.bind(this), this)); this.tempIcon.node.stopAllActions(); this.tempIcon.node.runAction(seq); this.flushNameLabel(); }, dealPlist(plistData) { if (!plistData) return; var cAn = null; var anClipList = this.animation.getClips(); for (var i = 0; i < anClipList.length; i++) { if (anClipList[i].name == this.data.icon) { cAn = anClipList[i]; break; } } if (cAn) { this.anName = cAn.name; this.runTempIconAction(true); // this.animation.play(cAn.name); return; } var iconDataList = []; for (var item in plistData.frames) { iconDataList.push(plistData.frames[item]); } if (iconDataList.length <= 0) return; var sps = []; for (var i = 0; i < iconDataList.length; i++) { var rect = iconDataList[i].rect; var sp = new cc.SpriteFrame(this.tex, cc.rect(rect.x, rect.y, rect.width, rect.width)); sps.push(sp); } var anClip = cc.AnimationClip.createWithSpriteFrames(sps, 6); anClip.wrapMode = cc.WrapMode.Loop; anClip.name = this.data.icon // cc.Animation this.anName = anClip.name; this.animation.addClip(anClip, anClip.name); // this.animation.play(anClip.name); this.tempAnimation.addClip(anClip, anClip.name); // this.tempAnimation.play(anClip.name); this.runTempIconAction(true); }, onClick() { if (this.isRunTempAn) return; if (!this.data) return; cc.log("onClick~~~~~"); var openType = this.data.open; if (!wb.wx.checkSupportMiniprogram()) openType = "image"; if (openType == "image") { wb.wx.previewImage({ urls: [this.data.pushImage] }) } else if (openType == "url") { } else if (openType == "miniprogram") { var tjKey = "ad_" + openType; var selfData = this.data; wb.wx.navigateToMiniProgram({ appId: selfData.pushAppID, path: selfData.openPath, extraData: { vigame_from: "miniprogram" }, success: function (res) { } }); } var tjData = { label: this.data.id, pushappid: this.data.pushAppID }; wb.DataTJManager.event2(this.pushType, tjData, this.placement); this.loopNext(); }, loopNext() { if(!wb.wxData.pushData) return; if (!this.isLoop) return; this.dataIndex++; if (this.dataIndex >= this.pushData.length) this.dataIndex = 0; this.data = this.pushData[this.dataIndex]; this.setData(this.data); }, // 统计参数 setPlacement(placement) { this.placement = placement; }, // 展示数上报 showPost() { var tjData = { label: this.data.id, pushappid: this.data.pushAppID, placement: this.placement }; wb.DataTJManager.reportData("pushShow", this.pushType, tjData); } }); module.exports = PushAdBtn;