关于5-11教程有无法处理的bug

来源:5-12 2d画布绘制能力在threejs中的整合

慕函数8913181

2019-02-15

老师你好,关于5-11我照着教程写有个bug解决不了。
图片描述
这种情况下,会报错scene为未定义。要是改变这两行的代码顺序则会报
图片描述
报错的原因是
this.texture.needsUpdate = true;
这一行代码。
下面是gameOverPage类的代码
`export default class GameOverPage {

constructor(callbacks) {
this.callbacks = callbacks;
}

init(options) {
console.log(‘gameOverPage.init’);
this.initGameOverCanvas(options);
}

//创建一个canvas绘制2D图片并将canvas当做纹理绘制到three.js的对象上
initGameOverCanvas(options) {
this.scene = options.scene;
this.camera = options.camera;

const aspect = window.innerHeight / window.innerWidth;
this.canvas = document.createElement('canvas');
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.texture = new THREE.Texture(this.canvas);

this.material = new THREE.MeshBasicMaterial({
  map: this.texture, 
  transparent: true,
  side: THREE.DoubleSide
})

this.geometry = new THREE.PlaneGeometry(window.innerWidth,window.innerHeight);
this.object = new THREE.Mesh(this.geometry,this.material);
this.object.position.z = 1;

this.context = this.canvas.getContext('2d');
this.context.fillStyle = '#333';
this.context.fillRect((window.innerWidth - 200) / 2, (window.innerHeight - 100) / 2, 200, 100);
this.context.fillStyle = '#eee'
this.context.font = '20px Georgia'
this.context.fillText('Game Over', (window.innerWidth - 200) / 2 + 50, (window.innerHeight - 100) / 2 + 55)
this.texture.needsUpdate = true;
this.scene.add(this.object);

}

show() {
console.log(‘gameOverPage.show’)
}
}`

写回答

1回答

千迦

2019-02-15

同学你好,这是因为imageBitmap在微信小游戏的环境里没有定义。可以在adapter中定义这么一个变量,等于一个空的function就可以解决报错,如何修改adapter在课程4-4中有介绍。简单处理的话,直接在代码的初始位置加上,GameGlobal.ImageBitmap = function() {} 即可。老师是在adapter里集成的。

0
0

three.js-打造微信小游戏跳一跳 快速掌握3D游戏开发

three.js+WebGL+微信小游戏API, 带你进入3D的游戏世界

793 学习 · 147 问题

查看课程