0
私はHTML5キャンバスを学ぶための小さなゲームを構築しており、同時に複数のイベントをトリガする問題に遭遇しました。キャンバス、撮影中に移動
私は上下に移動して撮影できる長方形のオブジェクトを持っています。問題は、スペースバーを押して(上下の矢印を押しながら)撮影すると、オブジェクトの動きが止まることです。スペースバーを押しながら移動を続けるにはどうすればいいですか?
let canvas = document.querySelector(`canvas`),
ctx = canvas.getContext(`2d`),
body = document.querySelector(`body`)
let shooter = {
y: canvas.height/2 - 25,
height: 50,
width: 20,
shots: [],
draw_shooter() {
ctx.strokeRect(5, this.y, this.width, this.height)
},
move_shooter(e) {
if (e.code === "ArrowUp")
if (this.y - 6 > 0) this.y -= 6;
if (e.code === "ArrowDown")
if (this.y + this.height + 6 < canvas.height) this.y += 6;
},
shoot() {
let shot = {
x: 30,
y: this.y + 20,
draw() {
//check if more, then kill in array
ctx.strokeRect(this.x, this.y, 20, 10)
this.x += 5;
}
}
this.shots.push(shot)
}
}
function update() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
shooter.draw_shooter()
shooter.shots.map((shot) => shot.draw())
requestAnimationFrame(update)
}
update()
body.addEventListener(`keydown`, (e) => shooter.move_shooter(e))
body.addEventListener(`keypress`, (e) => shooter.shoot(e))
<canvas width="500" height="200" style="border:1px solid black"></canvas>
あなたがHTMLとCSSも提供してくれれば素晴らしいと思いますゲーム全体をプロデュースする。 – Taurus