代码就是官方文档指南中的代码
无论是手机还是PC端,自动或者二维码预览,都停留在开始的loading...界面无法进入游戏
手机:坚果R1
安卓8.1.0
let canvas = wx.createCanvas();
console.log(canvas.width, canvas.height)
let ctx = canvas.getContext('2d')
ctx.fillStyle = 'green'
ctx.fillRect(canvas.width / 2 - 50, 0, 100, 100)
const { windowWidth, windowHeight } = wx.getSystemInfoSync()
function drawRect(x, y) {
ctx.clearRect(0, 0, windowWidth, windowHeight)
ctx.fillRect(x, y, 100, 100)
}
const rectX = canvas.width/2 - 50
let rectY = 0
let image = wx.createImage()
let imgLoad = false
let imgX = 0
let imgY = 50
image.src = 'images/plane.png'
image.onload = () => {
imgLoad = true
}
setInterval(()=>{
drawRect(rectX, rectY++)
if (imgLoad) {
ctx.drawImage(image, imgX, imgY, 50, 50)
}
if (imgX >= rectX - 100 && imgX <= rectX + 100 && imgY >= rectY - 100 && imgY <= rectY + 100) { // 飞机与矩形发生碰撞
wx.showModal({
title: '提示',
content: '发生碰撞,游戏结束!'
})
}
}, 16)
wx.onTouchMove((result) => {
imgX = result.changedTouches[0].clientX-25
imgY = result.changedTouches[0].clientY-25
})
我发现问题了。官方文档有错误。
需要这个
import './js/libs/weapp-adapter'
并且不能用wx.fn()这类方法
而是直接用H5的 new Image才行
我的1加手机copy你代码后无法复现你的BUG