用微信小程式寫了個類似消消樂的小程式。現在寫完了,在除錯,出現了個問題。
在開發平臺上通過點擊事件更改wxnl中的class的名字,
wxml: <view wx:for="{{wordMsg}}" wx:key="index" class='align' data-index ='{{index}}' catchtap="{{ item.flag ? 'gameda' : ' '}}">
<view class='{{item.classname}}' >
<text class='{{item.textfonname}}' style='margin-top:'>{{item.word1}}</text>
</view>
</view>
data: data: {
index: [{
ind: '0'
}, {
di: '0'
}],
wordMsg: [{
word1: "Gas\r\n Station",
classname: 'text-bg',
pipei: '1',
textfonname: 'text-fon',
flag:true
},
{
word1: "加油站",
classname: 'text-bg',
pipei: '-1',
textfonname: 'text-fon',
flag: true
},
事件代碼: gameda: function(e) {
console.log('點擊事件觸發')
// 點擊修改按鈕狀態
var that = this;
var g1 = parseInt(e.currentTarget.dataset.index)
var wordMsg = that.data.wordMsg
//改變選中狀態
if (wordMsg[g1].classname == 'text-bg') {
wordMsg[g1].classname = 'text-bg1';
} else {
wordMsg[g1].classname = 'text-bg';
}
that.setData({
wordMsg: wordMsg
})
//判斷邏輯開始
var index = that.data.index
var i = index[1].di
++i
index[1].di = i
//console.log(i)
wx.setStorage({
key: 'dianji',
data: index[1].di,
})
//console.log(wx.getStorageSync('dianji'))
var djcs = wx.getStorageSync('dianji')
//點擊次數驗證列印
//console.log(djcs)
//記錄點擊的按鈕
var index_1
var index_2
if ((djcs % 2) == 1) {
wx.setStorage({
key: 'index_1',
data: parseInt(e.currentTarget.dataset.index),
})
wx.setStorage({
key: 'pipeima_1',
data: wordMsg[g1].pipei,
})
// console.log('單次進入')
} else {
wx.setStorage({
key: 'index_2',
data: parseInt(e.currentTarget.dataset.index),
})
wx.setStorage({
key: 'pipeima_2',
data: wordMsg[g1].pipei,
})
// console.log('雙次進入')
}
if ((djcs % 2) != 0) {
// console.log('判斷二 單次進入')
// console.log('判斷二 單次進入 不做變化')
} else {
var value_3 = wx.getStorageSync('pipeima_1') //獲得第一次按鈕的匹配碼
console.log('第一次按鈕匹配碼:' + value_3)
var value_4 = wx.getStorageSync('pipeima_2') //獲得第二次按鈕的匹配碼
console.log('第二次按鈕匹配碼:' + value_4)
//console.log('判斷二 雙次進入')
value_3=parseInt(value_3)
value_4=parseInt(value_4)
var value_5 = value_3 + value_4
console.log('兩次匹配碼和:' + value_5)
if (value_5==0) {
console.log('判斷成功進入')
var value_1 = wx.getStorageSync('index_1') //獲得第一次按鈕的index
//console.log('第一次按鈕index:' + value_1)
var value_2 = wx.getStorageSync('index_2') //獲得第二次按鈕的index
//console.log('第二次按鈕index:' + value_2)
//進行樣式改變
wordMsg[value_1].classname = 'text-bg2';
wordMsg[value_2].classname = 'text-bg2';
wordMsg[value_1].flag = false;
wordMsg[value_2].flag = false;
console.log(wordMsg[value_1].flag, wordMsg[value_2].flag )
//資料改變并重繪
that.setData({
wordMsg: wordMsg
})
that.onLoad() //重點 重新執行下onLoad去獲取當前的資料
//延時0.5秒執行
setTimeout(function() {
//要延時執行的代碼
wordMsg[value_1].classname = 'text-bg';
wordMsg[value_2].classname = 'text-bg';
wordMsg[value_1].textfonname = 'text-fon1';
wordMsg[value_2].textfonname = 'text-fon1';
//存資料
that.setData({
wordMsg: wordMsg
})
//重繪
that.onLoad() //重新執行下onLoad去獲取當前的資料
//快取清除
wx.clearStorage()
}, 500) //延遲時間 這里是0.5秒
//記錄成功幾次
var index = that.data.index
var ii = index[0].ind
++ii
index[0].ind = ii
if (ii == 8) {
wx.reLaunch({
url: '../win/win',
})
console.log('通關') //--------------------------------------------------------------添加跳轉通關頁面
}
} else {
console.log('判斷失敗進入')
var value_1 = wx.getStorageSync('index_1') //獲得第一次按鈕的index
var value_2 = wx.getStorageSync('index_2') //獲得第二次按鈕的index
wordMsg[value_1].classname = 'text-bg3';
wordMsg[value_2].classname = 'text-bg3';
//資料改變并重繪
that.setData({
wordMsg: wordMsg
})
that.onLoad() //重點 重新執行下onLoad去獲取當前的資料
//延時0.5秒執行
setTimeout(function() {
//要延時執行的代碼
wordMsg[value_1].classname = 'text-bg';
wordMsg[value_2].classname = 'text-bg';
//存資料
that.setData({
wordMsg: wordMsg
})
//重繪
that.onLoad() //重新執行下onLoad去獲取當前的資料
//快取清除
wx.clearStorage()
}, 500) //延遲時間 這里是0.5秒
}
}
},
然后我把代碼上傳了,體驗版用ios的手機能用,用安卓手機用,游戲界面點擊了就報錯,但是在開發平臺上的真機除錯中,ios與安卓都可以用。
報錯是: thirdScriptError
Cannot set property 'classname' of undefined; [Component] Event Handler Error @ pages/games/game#bound gameda
TypeError: Cannot set property 'classname' of undefined
at ye.gameda (http://127.0.0.1:48173/appservice/pages/games/game.js:270:36)
at Object.r.safeCallback (WAService.js:1:857898)
at WAService.js:1:1048426
at r (WAService.js:1:982076)
at WAService.js:1:982184
at WAService.js:1:253666
at n (http://127.0.0.1:48173/appservice/appservice?t=1562141842045:1195:27752)
at e.exports.<anonymous> (http://127.0.0.1:48173/appservice/appservice?t=1562141842045:1195:28094)
at http://127.0.0.1:48173/appservice/appservice?t=1562141842045:1195:13287
at Array.forEach (<anonymous>)
然后就是,該程式邏輯是點擊兩個相匹配的中文和英文會先變綠再消失,有個0.5秒左右的程序。 再這個程序之間如果點擊了其他的中文或者單詞就會報錯,和剛才那個錯是一樣的,不在這0.5秒之間點,就不會報錯。
請問我是那點錯了怎么改
uj5u.com熱心網友回復:
洗掉延時函式之后第二個問題的報錯沒有了現在就剩下第一個問題了,真機除錯和體驗版,ios都能行,但安卓的真機除錯沒問題,體驗版就有問題轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/130699.html
標籤:微信開發
上一篇:麻煩哪位大神幫忙解決一下
下一篇:手機修改串號
