标题说明了一切,我试图根据它所处的角度向前移动一个对象。
这是我的相关代码:
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,但对象看起来仍然不像向右移动,尽管相机确实如此.