我有一個對象,我想在按兩個鍵時對角移動。到目前為止它可以工作,但是在釋放兩個鍵后,如果我只按一個鍵,即's',它會一直沿對角線移動。僅當同時按下兩個鍵時,如何使對象沿對角線移動?let keysPressed = ["d", "s"];const box1 = document.getElementById("box1");var box1x = box1.offsetLeft;var box1y = box1.offsetHeight;document.addEventListener('keydown', (event) => {keysPressed[event.key] = true;if (keysPressed['d'] && event.key == 's') { box1.style.left = document.getElementById("box1").offsetLeft + 5 + 'px'; box1.style.top = document.getElementById("box1").offsetTop + 5 + 'px';} });
如何解決此 if 語句
慕田峪7331174
2022-10-08 14:57:36