Все остальные части в порядке. функция willImpactMovingLeft работает нормально и должна быть зеркалом willImpactMovingRight, но right не работает.
простой рабочий код для этого примера:
Код: Выделить всё
var runSpeed = 5;var CUBE = document.getElementById("cube");var immovables = ["ground1", "ground2"];let Key = {pressed: {},left: "ArrowLeft",right: "ArrowRight",isDown: function (key){return this.pressed[key];},keydown: function (event){this.pressed[event.key] = true;},keyup: function (event){delete this.pressed[event.key];}}
window.addEventListener("keyup", function(event) {Key.keyup(event);});
window.addEventListener("keydown", function(event) {Key.keydown(event);});
setInterval(()=>{
if (Key.isDown(Key.left)){
if(willImpactMovingLeft("cube", immovables)!=false){
cube.style.left = willImpactMovingLeft("cube", immovables)+"px";
}else{
cube.style.left = CUBE.offsetLeft - runSpeed +"px";
}
}
if (Key.isDown(Key.right)){
if(willImpactMovingRight("cube", immovables)!=false){
cube.style.left = willImpactMovingRight("cube", immovables)+"px";
}else{
cube.style.left = CUBE.offsetLeft + runSpeed +"px";
}
}
}, 10);
function willImpactMovingLeft(a, b){
var docA = document.getElementById(a);
var docB = document.getElementById(b[0]);
for(var i=0;idocB.offsetTop&&docA.offsetTopdocB.offsetTop&&docA.offsetTop+docA.offsetHeightdocB.offsetLeft+runSpeed){
if(docA.offsetLeft-runSpeed
Подробнее здесь: [url]https://stackoverflow.com/questions/78250108/character-phases-through-wall-one-way-but-not-the-other[/url]