【发布时间】:2018-02-18 10:52:08
【问题描述】:
我正在尝试制作一个鸟类游戏,其中我只旋转我通过游戏类绘制的单个鸟类图像(底部的最后一个函数)。有什么方法可以让我的鸟在跳跃和落下时旋转 30 度角?我不想只为我的鸟创建另一个画布。目前,除非我不在跳转功能中恢复,否则没有任何效果。
class Bird extends Sprite {
constructor(options, game) {
super(options);
this.image = options.image;
this.sX = options.sX;
this.sY = options.sY;
this.sWidth = options.sWidth;
this.sHeight = options.sHeight;
this.dX = options.dX;
this.dY = options.dY;
this.dWidth = options.dWidth;
this.dHeight = options.dHeight;
this.frameIndex = 1;
this.tickCount = 0;
this.ticksPerFrame = 10;
this.numFrames = 8;
this.game = game;
this.birdRight = this.dX + this.dWidth;
}
jump(e, ctx) {
ctx.save();
ctx.translate(this.xPos + this.width / 2, this.yPos + this.height / 2);
ctx.rotate(this.dY * 15 * Math.PI / 360);
ctx.restore();
this.dY -= 40;
this.sX = 0;
}
fall(ctx) {
this.tickCount += 1;
if (this.tickCount > this.ticksPerFrame) {
this.sX = 27;
this.tickCount = 0;
}
if (this.dY < 450 ) {
this.dY += 1.5;
} else {
this.dY = this.dY;
}
// this.sX = (this.frameIndex * this.sWidth) / (this.numFrames);
}
draw(ctx) {
ctx.clearRect(0, 0, Game.DIM_X, Game.DIM_Y);
this.allObjects().forEach((object) => {
object.draw(ctx);
});
}
【问题讨论】:
标签: javascript html animation canvas