【发布时间】:2016-01-22 04:16:08
【问题描述】:
我正在使用 ThreeJs 制作俄罗斯方块类型的演示。对于渲染,我使用requestAnimationFrame。片段:
game.prototype.render = function(){
var thisObj = this;
requestAnimationFrame( function() {thisObj.render();} );
var now = new Date().getTime();
var dt = now - (time || now);
time = now;
this.update(dt);
this.boardMgr.render(dt);
this.renderer.render(this.scene, this.camera);
};
this.update(dt) - 调用 boardMgr.update(dt)
BoardMgr.prototype.update = function(dt) {
if(this.activeBlock == null){
this.activeBlock = this.getNextBlock();
//console.log("letter: " + this.activeBlock.letter);
}
// update active block as per keyboard input
// left/right/falldown
// else
{
this.move(this.activeBlock, DIRECTION.DOWN);
}
};
当我运行它时,块下降得太快(如果我理解正确,更新被调用太多次,因此块被更新得很快)。
如何控制下落方块的速度?或者我不应该经常调用更新?正确的处理方法是什么?
代码可以在这里看到: https://github.com/brainydexter/PublicCode/tree/master/graphics
运动:我有一个 5x5 的网格板,我保持一个板的位置。因此,如果块can 向下移动,则新位置为:(x, y+1)。在渲染时,我根据棋盘位置更新块在世界坐标系中的位置。所以,我总是以 BLOCK_WIDTH 为增量移动块:
BoardMgr.prototype.render = function(dt) {
for (var i = 0; i < this.blocks.length; i++) {
if(this.blocks[i].visible)
{
this.blocks[i].position.x = (this.BLOCK_WIDTH/2) + ( this.blocks[i].boardPosition.x * this.BLOCK_WIDTH);
this.blocks[i].position.y = (this.BLOCK_WIDTH/2) + ( this.blocks[i].boardPosition.y * this.BLOCK_WIDTH);
}
};
};
【问题讨论】:
标签: javascript 3d three.js 2d