page.onTouchStart = function(e){
let x = parseInt(e.touches[0].pageX / _this.width), y = parseInt(e.touches[0].pageY / _this.height);
let val = _this.newTypeArr[y][x];
_this.cval = _this.newTypeArr[y][x];
_this.page.setData({
status: true,
currentX: val.x * _this.width,
currentY: val.y * _this.height,
currentPX: val.px,
currentPY: val.py
})
_this.originX = val.x * _this.width;
_this.originY = val.y * _this.height;
_this.originPX = val.px;
_this.originPY = val.py;
_this.currentX = e.touches[0].pageX;
_this.currentY = e.touches[0].pageY;
}
page.onTouchMove = function(e){
let x = parseInt(e.touches[0].pageX / _this.width), y = parseInt(e.touches[0].pageY / _this.height);
let cx = e.touches[0].pageX, cy = e.touches[0].pageY;
let cgx = cx - _this.currentX, cgy = cy - _this.currentY;
_this.val = _this.newTypeArr[y][x];
_this.page.setData({
status: true,
currentX: _this.originX + cgx,
currentY: _this.originY + cgy,
currentPX: _this.originPX,
currentPY: _this.originPY
})
}
page.onTouchEnd = function(e){
_this.cval.px = _this.val.px;
_this.cval.py = _this.val.py;
_this.val.px = _this.originPX;
_this.val.py = _this.originPY;
_this.page.setData({
imgPoints: _this.newTypeArr,
status: false,
currentX: 0,
currentY: 0,
currentPX: 0,
currentPY: 0
})
_this.originX = 0;
_this.originY = 0;
_this.originPX = 0;
_this.originPY = 0;
_this.currentX = 0;
_this.currentY = 0;
if(_this.checkWin()){
let text = '恭喜您,您已成为' + page.data.levelArr[_this.type - 2].text + '的人!是否继续闯关?';
wx.showModal({
title: '过关提醒',
content: text,
success(res) {
if (res.confirm){
_this.type++;
_this.init();
} else if (res.cancel){
wx.showToast({
title: '您将永远成为一个' + page.data.levelArr[_this.type - 2].text,
success(){
wx.navigateBack({
delta: 1
})
}
})
}
}
})
};
}
}
通过 JSON.stringify 方法将 this.typeArr 和 this.newTypeArr 转化比较,判断是否拼图成功!
checkWin(){
return JSON.stringify(this.typeArr) === JSON.stringify(this.newTypeArr);
}