x和y的画布旋转效果

问题描述

在使图像朝着所面对的方向移动之前,我已经取得了效果。但是现在当我尝试时,图像旋转了,但是没有朝正确的方向移动。 如何使画布中的图像朝其面对的方向移动?

我的代码

    ctx.save();
    ctx.translate(bulletArray[i].x,bulletArray[i].y);
    ctx.rotate(bulletArray[i].degree);
    ctx.translate(-bulletArray[i].x,-bulletArray[i].y);
    bulletArray[i].x++;
    ctx.drawImage(bullet,bulletArray[i].x,bulletArray[i].y,10,10);
    ctx.restore();

(这是在游戏循环中,因此会重复)

解决方法

如果您不想轮换,请不要使用

ctx.rotate(bulletArray[i].degree);

在您的代码中

,

好的。它不旋转的原因是旋转点必须位于屏幕的中心而不是子弹。这将使子弹正确移动,而不仅仅是旋转。