我正在使用canvas和JavaScript创建一个横向滚动的无尽太空主题游戏。我只使用上下箭头来控制飞船,并希望实现某种移动缓动效果,以便当我放开键时,飞船不会立即停止。我已经搜索了一些内容,但是没有找到任何有用的东西,而我的尝试也没有成功。这是我尝试过的方法。
Jet.prototype.checkDirection = function () {
if (this.isUpKey) {
this.drawY -= this.speed;
if (this.speed < 5) {
this.speed += 0.1;
}
}
if (this.isDownKey) {
this.drawY += this.speed;
if (this.speed < 5) {
this.speed += 0.1;
}
}
if (!this.isUpKey) {
if (!this.isDownKey) {
if (this.speed >= 0) {
this.drawY -= this.speed;
this.speed -= 1;
}
}
}
if (!this.isDownKey) {
if (!this.isUpKey) {
if (this.speed >= 0) {
this.drawY += this.speed;
this.speed -= 1;
}
}
}