// pages/luckyStar/luckyStar.js const app = getApp() Page({ /** * 页面的初始数据 */ data: { imgUrl: app.globalData.urlStatic,//图片路径 ruleShow: false,//是否显示游戏玩法 ruleCloseShow: false,//是否显示游戏玩法关闭按钮 clawTop:95,//爪子的top值 clawLeft: 293,//爪子的left值 clawScale: 1,//爪子的scale值 downNum:30,//倒计时时间 setInt:null,//倒计时元素 pizeTip:1,//抓奖提示框 setGroup:{ left:null, right: null, top: null, bottom: null, }, gameSign: null, //游戏结束时需要 startType:1,//开始类型:1通过游戏玩法里的开始按钮,2:通过抓取按钮 gameState:false,//游戏状态 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { app.globalData.nowPage = 3; }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { return app.sharePack(); }, closeRule:function(){//关闭游戏玩法 this.setData({ ruleShow:false }) }, openGameRule: function () {//打开游戏玩法 if(!this.data.gameState){ this.setData({ ruleShow: true }) } }, gameStart: function () {//开始游戏按钮 if(this.data.startType==1){ this.data.startType = 2; this.setData({ ruleShow: false, ruleCloseShow: true }) this.beginGame(); } }, beginGame: function () {//开始游戏 app.wxRequest(app.globalData.urlRoot + "dollGame/beginGame", {}, res => { console.log(res); if(res.code==200){ this.data.gameSign = res.data.sign; this.endGame(); if (this.data.gameSign) { this.downTimeFun(); } }else{ wx.showToast({ title: res.msg, icon:"none" }) } },this) }, endGame:function(){//结束游戏 app.wxRequest(app.globalData.urlRoot + "dollGame/endGame", { sign:this.data.gameSign}, res => { console.log(res); if(res.code=200){ }else{ console.log(res); } }, this,"POST") }, startClaw:function(e){//开始控制爪子方向 let direction = e.currentTarget.dataset.direction; if (direction == "L") {//向左 if (this.data.clawLeft <= 130) { return; } this.data.setGroup.left = setInterval(() => { this.setData({ clawLeft: this.data.clawLeft-=2 }) if (this.data.clawLeft <= 130) { clearInterval(this.data.setGroup.left); } },20); } else if (direction == "R") {//向右 if (this.data.clawLeft >= 445) { return; } this.data.setGroup.right = setInterval(() => { this.setData({ clawLeft: this.data.clawLeft += 2 }) if (this.data.clawLeft >= 445) { clearInterval(this.data.setGroup.right); } }, 20); } else if (direction == "T") {//向后 if (this.data.clawScale <= 0.5) { return; } this.data.setGroup.top = setInterval(() => { this.setData({ clawScale: this.data.clawScale -= 0.01 }) if (this.data.clawScale <= 0.5) { clearInterval(this.data.setGroup.top); } }, 20); } else if (direction == "B") {//向前 if (this.data.clawScale >= 1.5) { return; } this.data.setGroup.bottom = setInterval(() => { this.setData({ clawScale: this.data.clawScale += 0.01 }) if (this.data.clawScale >= 1.5) { clearInterval(this.data.setGroup.bottom); } }, 20); } }, endClaw:function(e){//结束爪子动作 let direction = e.currentTarget.dataset.direction; if (direction == "L") {//向左 clearInterval(this.data.setGroup.left); } else if (direction == "R") {//向右 clearInterval(this.data.setGroup.right); } else if (direction == "T") {//向后 clearInterval(this.data.setGroup.top); } else if (direction == "B") {//向前 clearInterval(this.data.setGroup.bottom); } }, getClaw:function(){//抓取 this.setData({ clawTop: this.data.clawScale > 1 ? 490 - (2 * (this.data.clawScale-1) * 90) : 490 + (2 * (1 - this.data.clawScale) * 90 ) }) this.closeSetInt(); }, downTimeFun:function(){//游戏倒计时 this.data.gameState = true; this.data.setInt = setInterval(()=>{ this.data.downNum -= 1; if (this.data.downNum<10){ this.data.downNum = '0' + this.data.downNum; } this.setData({ downNum: this.data.downNum }) if (this.data.downNum < 1) { this.closeSetInt(); } },1000); }, closeSetInt: function () {//关闭倒计时 this.data.gameState = false; clearInterval(this.data.setInt); this.setData({ downNum: 30, clawTop: this.data.clawScale > 1 ? 455 - (2 * (this.data.clawScale - 1) * 90) : 455 + (2 * (1 - this.data.clawScale) * 90) }) setTimeout(()=>{ this.setData({ downNum: 30, clawTop: 95, clawLeft: 293, clawScale:1 }) }, 1200); }, prizeLook:function(){//活动奖品 if(!this.data.gameState){ wx.navigateTo({ url: '../prizes/prizes' }) } } })