我是three.js 的新手,我正在制作一個保齡球游戲。但是我在這里有一個問題,我在一個類中做了物理,現在我需要從我的“應用程式”類中訪問一個函式。我真的不明白這里的問題,我很迷茫。
應用類:
export class Application {
constructor() {
this.objects = [];
this.createScene();
}
createScene() {
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(60,
window.innerWidth / window.innerHeight, 1, 10000);
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(this.renderer.domElement);
this.render();
}
getMesh(){
return this.curveObject;
}
update(){
}
render() {
requestAnimationFrame(() => {
this.render();
});
this.objects.forEach((object) => {
object.update();
});
this.renderer.render(this.scene, this.camera);
}
}
“物理”類中的影片函式:
animate(){
if (this.pinTest){
// console.log(this.pinTest);
this.pin1Mesh.position.copy(this.pin1Body.position);
this.pin1Mesh.quaternion.copy(this.pin1Body.quaternion);
this.pinTest.position.copy(this.pin1Body.position);
this.pinTest.quaternion.copy(this.pin1Body.quaternion);
}
// I need to call render here
//this.renderer.render(this.scene, this.camera);
}
uj5u.com熱心網友回復:
基類示例:
顯示代碼片段
class baseAnimation {
animate() {
if (this.pinTest) {
// console.log(this.pinTest);
this.pin1Mesh.position.copy(this.pin1Body.position);
this.pin1Mesh.quaternion.copy(this.pin1Body.quaternion);
this.pinTest.position.copy(this.pin1Body.position);
this.pinTest.quaternion.copy(this.pin1Body.quaternion);
} else
console.log("pinTest === false");
}
}
class Physics extends baseAnimation {
}
let test = new Physics();
test.pinTest = false;
test.animate();
將物件傳遞給Animation類(使用靜態方法)示例:
顯示代碼片段
class Animation {
static animate(obj) {
if (obj.pinTest) {
// console.log(this.pinTest);
obj.pin1Mesh.position.copy(obj.pin1Body.position);
obj.pin1Mesh.quaternion.copy(obj.pin1Body.quaternion);
obj.pinTest.position.copy(obj.pin1Body.position);
obj.pinTest.quaternion.copy(obj.pin1Body.quaternion);
} else
console.log("pinTest === false");
}
}
class Physics {
}
let test = new Physics();
test.pinTest = false;
Animation.animate(test);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/494235.html
標籤:javascript 功能 班级 三.js
上一篇:如何從函式C 回傳動態字符陣列
