【发布时间】:2020-08-20 11:22:12
【问题描述】:
我的申请有点问题。我试图制作僵尸从右侧到左侧的游戏。他们已经在移动了,我也可以用鼠标点击杀死他们,但我找不到一种方法,我怎么能把致命线放在他们会死的地方。我试图做抵消,但它不起作用。代码如下:
const game_window = document.querySelector('.game-container')
const score_window = document.querySelector('.score')
const hp = document.querySelector('.hp')
let points = 0;
class Game {
constructor(monster_height, monster_width) {
this.monster_height = monster_height
this.monster_width = monster_width
this.declare()
}
declare() {
this.new_monster
}
create_monster() {
this.new_monster = document.createElement('div')
this.new_monster.classList.add('monster')
game_window.appendChild(this.new_monster)
}
monster_click() {
let monsters = document.querySelectorAll('.monster')
monsters.forEach(el => {
el.addEventListener('click', function() {
game_window.removeChild(el)
this.score()
}.bind(this));
});
};
monster_move() {
console.log(this.new_monster);
this.new_monster.style.transform = "translateX(-1500px)"
}
score() {
points += 10
score_window.innerText = points;
}
health() {
}
}
letsPlay = new Game(50, 50)
setInterval(() => {
letsPlay.monster_move()
}, 500);
setInterval(() => {
letsPlay.create_monster()
letsPlay.monster_click()
letsPlay.health()
}, 1000);
//500
//1000
<div class="game-container"></div>
<span class="score"></span>
<span class="hp"></span>
【问题讨论】:
-
欢迎来到 StackOverflow。对于您的问题,我们将需要更多信息和代码。 “致命线”是什么意思?您能否也分享随之而来的 HTML 和 CSS,因为这可能会影响答案。理想情况下,如果您点击“JavaScript/HTML/CSS Snippet”按钮(看起来像上面带有 的纸)并将部件放在那里,您可以让它在浏览器中运行,这将更容易提供帮助。
-
我给你做了一个sn-p。你能把它改成minimal reproducible example吗?
标签: javascript pixel offset