123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- import util from '../../utils/util'
- var app = getApp()
- Page({
- data: {
- headUrl: "",
- GAMETIME: 60,
- time: 0,
- mark: 0,
- ratio: 0,//比分
- gaming: false,
- rows: [],
- modal: {
- hidden: true
- },
- lastTouchIndex: -1,
- playVoide: true,
- audios: [],
- audiosLength: 0,
- currentAudio: 0,
- innerAudioContext: null,
- speed: 2,
- redMark: 0,
- overColIndex: -1,
- overRowIndex: -1,
- showContinue: false,
- showContinueTimes: 0,
- showContinueTimesFlag: 1,
- timeOver: false,
- overSharedFlag: false,
- overFlag: false,
- errorFlag: false,
- timerFlag: false,
- continueMsg: "求群好友帮你复活~",
- shareMsg: "我离音乐大师一步之遥,快来帮我挑战吧~",
- userId: wx.getStorageSync('userId'),
- },
- onLoad: function () {
- var that = this;
- wx.getStorage({
- key: 'userId',
- success: function(res) {
- that.setData({
- userId: res.data
- })
- },
- })
-
- wx.showShareMenu({
- withShareTicket: true //要求小程序返回分享目标信息
- })
- var userInfo = wx.getStorageSync("userInfo");
- this.setData({
- headUrl: userInfo.avatarUrl
- })
- this.data.innerAudioContext = wx.createInnerAudioContext();
- console.log('进入游戏页面')
- this.initData()
- },
- onShow: function () {
- // this.initData();
- },
- /**
- * 初始化数据
- */
- initData() {
- var random = Math.floor(Math.random() * 5);
- var audiosData = app.data.audios[random];
- var audiosDataLengh = audiosData.length;
- // 初始化方格数据
- var rows = []
- for (let i = 0; i < 4; i++) {
- var row = {
- blackIndex: util.random(1, 4, true)
- }
- rows.push(row)
- }
- // // 初始化时间
- // var time = this.data.GAMETIME
- // this.setData({
- // rows: rows,
- // time: time,
- // currentAudio: 0,
- // mark: 0,
- // audios: audiosData,
- // audiosLength: audiosDataLengh
- // })
- // 初始化时间
- var time = this.data.GAMETIME
- this.setData({
- rows: rows,
- redMark: 0
- });
- if (!this.data.showContinue) {
- this.setData({
- time: time,
- currentAudio: 0,
- mark: 0,
- audios: audiosData,
- audiosLength: audiosDataLengh,
- });
- } else {
- this.setData({
- showContinue: false,
- gaming: false
- });
- }
- },
- next(event) {
- var rowIndex = event.currentTarget.dataset.rowIndex - 0
- var colIndex = event.currentTarget.dataset.colIndex - 0
- if (rowIndex !== this.data.rows.length - 1) {
- return true;
- }
- // 如果当前游戏还没有开始 则开始游戏
- if (!this.data.gaming) {
- this.startGame()
- }
- if(this.data.timerFlag){
- this.startGame();
- }
- if (this.data.playVoide) {
- this.play()
- }
- // 加一分
- var mark = this.data.mark + 1;
- this.setData({
- lastTouchIndex: colIndex,
- mark: mark
- })
- var rows = this.data.rows
- let newRow = {
- blackIndex: util.random(1, 4, true)
- }
- rows.unshift(newRow)
- rows.pop()
- this.setData({
- rows: rows
- })
- // console.log(event)
- },
- play() {
- var context = wx.createInnerAudioContext();
- context.autoplay = true;
- context.src = this.data.audios[this.data.currentAudio];
- context.onPlay(() => {
- console.log('开始播放')
- })
- context.onError((res) => {
- console.log(res.errMsg)
- console.log(res.errCode)
- })
- context.onEnded(() => {
- context.destroy();
- })
- var currentAudioMark = this.data.currentAudio + 1;
- if (currentAudioMark >= this.data.audiosLength) {
- currentAudioMark = 0;
- }
- this.setData({
- currentAudio: currentAudioMark
- })
- },
- //播放音乐回调
- onplay() {
- console.log('音效已经播放')
- },
- // 开始游戏
- startGame() {
- this.setData({
- gaming: true,
- timerFlag: false
- });
- this.tId = setInterval(() => {
- console.log(this.data.time);
- var time = this.data.time - 1
- this.setData({
- time: time
- })
- if (this.data.time === 0) {
- if (this.data.overSharedFlag){
- this.setData({
- overFlag: true,
- })
- this.gameOver()
- } else {
- this.setData({
- overSharedFlag: true,
- overFlag: false,
- errorFlag: true,
- timerFlag: true
- })
- this.gameOver()
- var timePlus = this.data.time + 10;
- if (timePlus >= this.data.GAMETIME) {
- timePlus = this.data.GAMETIME;
- }
- this.setData({
- showContinue: true,
- time: timePlus
- })
- }
- }
- var ratio = (this.data.GAMETIME - this.data.time) / this.data.GAMETIME;
- this.setData({
- ratio: ratio
- })
- this.onReady();
- }, 500)
- },
- //点击错误时间
- over(event) {
- if (!this.data.gaming) {
- return
- }
- var rowIndex = event.currentTarget.dataset.rowIndex - 0
- var colIndex = event.currentTarget.dataset.colIndex - 0
- this.setData({
- redMark: 1,
- overColIndex: colIndex,
- overRowIndex: rowIndex
- })
- if (this.data.errorFlag) {
- this.setData({
- overFlag: true,
- })
- this.gameOver()
- } else {
- this.setData({
- overSharedFlag: false,
- overFlag: false,
- errorFlag: true,
- timerFlag: true
- })
- this.gameOver()
- var timePlus = this.data.time + 10;
- if(timePlus >= this.data.GAMETIME){
- timePlus = this.data.GAMETIME;
- }
- this.setData({
- showContinue: true,
- time: timePlus
- })
- }
- },
- /**
- * 结束游戏
- */
- gameOver() {
- this.setData({
- gaming: false,
- })
- clearInterval(this.tId)
- this.onShowModal()
- },
- reStartGame() {
- this.setData({
- 'modal.hidden': true,
- ratio: 0,
- currentAudio: 0
- })
- this.initData()
- },
- //答题错误
- onShowModal() {
- var that = this;
- this.setData({
- 'modal.hidden': false,
- })
- wx.setStorage({
- key: 'chanOneScore',
- data: that.data.mark
- })
- wx.getStorage({
- key: 'chanScore',
- success: function (res) {
- if (res.data <= that.data.mark) {
- //更新最高成绩
- wx.setStorage({
- key: 'chanScore',
- data: that.data.mark
- })
- app.reportGameScore('chan',that.data.mark);
- };
- console.log("showModal"+that.data.overFlag)
- if (that.shareOverAndRedirect()) {
- return;
- }
- if (that.data.overFlag) {
- that.setData({
- showContinue: false
- })
- wx.redirectTo({
- url: '/pages/layer/layer',
- })
- } else {
- if (that.data.showContinueTimesFlag >= -1){
- that.setData({
- showContinue: true
- })
- } else {
- wx.redirectTo({
- url: '/pages/layer/layer',
- })
- }
- }
- },
- fail: function (res) {
- //更新最高成绩
- wx.setStorage({
- key: 'chanScore',
- data: that.data.mark
- })
- app.reportGameScore('chan',that.data.mark);
- console.log("showModal" + that.data.overFlag)
- if (that.shareOverAndRedirect()) {
- return;
- }
- if (that.data.overFlag) {
- that.setData({
- showContinue: false
- })
- wx.redirectTo({
- url: '/pages/layer/layer',
- })
- } else {
- if (that.data.showContinueTimesFlag >= -1) {
- that.setData({
- showContinue: true
- })
- } else {
- wx.redirectTo({
- url: '/pages/layer/layer',
- })
- }
- }
- }
- })
- },
- onReady: function () {
- // 页面渲染完成
- var cxt_arc = wx.createCanvasContext('canvasArc');//创建并返回绘图上下文context对象。
- cxt_arc.setLineWidth(6);
- cxt_arc.setStrokeStyle('#3ea6ff');
- cxt_arc.setLineCap('round')
- cxt_arc.beginPath();//开始一个新的路径
- cxt_arc.arc(106, 53, 30, 0, 2 * Math.PI, false);//设置一个原点(106,106),半径为100的圆的路径到当前路径
- cxt_arc.stroke();//对当前路径进行描边
- cxt_arc.setLineWidth(6);
- cxt_arc.setStrokeStyle('#d2d2d2');
- cxt_arc.setLineCap('round')
- cxt_arc.beginPath();//开始一个新的路径
- cxt_arc.arc(106, 53, 30, -Math.PI * 1 / 2, -Math.PI * 1 / 2 + 2 * Math.PI * this.data.ratio, false);
- cxt_arc.stroke();//对当前路径进行描边
- cxt_arc.draw();
- },
- //放弃
- giveUp: function () {
- wx.redirectTo({
- url: '/pages/layer/layer',
- });
- },
- //右上角转发
- onShareAppMessage() {
- var that = this;
- return {
- title: this.data.shareMsg,
- path: '/pages/home/home',
- imageUrl: app.data.shareIcon[1],
- success: function (res) {
- console.log(res)
- console.log(res.shareTickets)
- // 转发成功
- if (!res.shareTickets || res.shareTickets.length == 0) {
- wx.showToast({
- title: '找不到群好友~',
- })
- } else {
- wx.showToast({
- title: '复活成功~',
- })
- var continueTimes = that.data.showContinueTimesFlag - 1;
- if (continueTimes <= 0) {
- that.initData();
- }
- that.setData({
- showContinueTimesFlag: continueTimes
- })
- }
- }
- }
- },
- //判断弹窗次数,如果已经分享过一次,则不再弹出,直接跳转到结果页面
- shareOverAndRedirect: function() {
- console.log(this.data.showContinueTimesFlag + " " + this.data.overFlag)
- if (this.data.showContinueTimesFlag < 0 && this.data.overFlag){
- wx.redirectTo({
- url: '/pages/layer/layer',
- })
- return true;
- }
- },
- //上报成绩事件
- // reportGameScore: function() {
- // var that = this;
- // app.func.req('/blackwhite/reportGameScore', 'get', "userId=" + this.data.userId, {
- // userId: that.data.userId,
- // channel: app.data.channel,
- // gameType: 'jingdian',
- // score: 99
- // },(res) => {
- // console.log(res)
- // })
- // }
- })
|