【发布时间】:2013-10-21 17:04:00
【问题描述】:
标题说明了一切,我试图根据物体所处的角度向前移动一个物体。
这是我的相关代码:
xView = this.x-this.width/2;
yView = this.y-this.height/2;
playerCtx.drawImage(imgSprite, 0, 0, this.width, this.height, xView, yView, this.width, this.height);
playerCtx.save();
playerCtx.clearRect(0, 0, game.width, game.height);
playerCtx.translate(xView, yView);
playerCtx.rotate(angle *Math.PI/180);
playerCtx.drawImage(imgSprite, 0, 0, this.width, this.height, -xView, -yView, this.width, this.height);
playerCtx.restore();
}
if(Game.controls.left) {
angle-=1;
if(Game.controls.up){
this.x += this.speed * Math.cos(angle * Math.PI / 180);
this.y -= this.speed * Math.sin(angle * Math.PI / 180);
}
对象不移动对应var angle。
编辑
我不知道为什么我的代码不起作用,所以我改用了一个包含 36 个不同角度的精灵表。这行得通,但是旋转太快了。如果有人能告诉我为什么以上不能正常工作,或者我将如何使以下功能变慢:
if(Game.controls.right) {
currentFrame++;
angle+=10;
}
“慢”是指按住左键时,angle+10; currentFrame++; 会升高到快,添加更多帧可能需要很长时间。
编辑
为我原来的问题添加了一个Jfiddle,角度变量随着旋转而移动,例如如果对象面向右,角度将等于90,但对象仍然没有看起来它在向右移动,尽管相机确实如此。
【问题讨论】:
标签: javascript canvas rotation html5-canvas 2d