简体   繁体   English

告诉我,为什么Three.js对象不移动?

[英]Why won't my Three.js object move when I tell it to?

I am trying to create a three.js block that will move when I press different keys. 我正在尝试创建一个three.js块,当我按下不同的键时该块将移动。 I have been successful with creating a functional eventListener , checking using console.log() , and also been successful with moving my block, but when I put both together, the block doesn't move. 我已经成功创建了一个功能eventListener ,并使用console.log()检查,并且成功移动了我的代码块,但是当我将两者放在一起时,代码块就不会移动。 I've also tried using the successful block moving code and looping it so I can see that it moves on command, and the block has __dirtyPosition set to true, but even that didn't move it. 我还尝试使用成功的代码块移动代码并对其进行循环,以便可以看到它在命令上移动,并且代码块的__dirtyPosition设置为true,但即使这样也没有移动它。

I'm a fairly experienced programmer, and have created many working programs like this one, but for some reason it just isn't working, even though I've compared it to my working programs and nothing's different. 我是一个非常有经验的程序员,并且已经创建了许多像这样的工作程序,但是由于某种原因,即使我已经将其与我的工作程序进行了比较,但它仍然无法正常工作。

var block1 = new Physijs.ConvexMesh(
  new THREE.CubeGeometry(5, 5, 5),
  new THREE.MeshBasicMaterial({color: 0x444444})
);
block1.position.set(0, 0, 0);
block1.__dirtyPosition = true;
scene.add(block1);

the eventListener : eventListener

document.addEventListener("keydown", function(event) {
  var code = event.keyCode;
  //a, s, w, d respectively
  if (code === 65) block1.translateX(-3);
  if (code === 83) block1.translateY(-3);
  if (code === 87) block1.translateY(3);
  if (code === 68) block1.translateX(3);
});

and the renderer : renderer

Top of the code: 代码顶部:

var renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

Bottom of the code: 代码底部:

renderer.render(scene, camera);

Am I missing anything? 我有什么想念的吗?

Thanks to @Radio, I realized I had completely forgotten to loop the renderer. 感谢@Radio,我意识到我完全忘记了循环渲染器。 I just changed this 我刚刚改变了这个

renderer.render(scene, camera);

to

function animate() {
  renderer.render(scene, camera);
  setTimeout(animate, 20);
}
animate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM