移动物体向下的回调函数渲染不正确。

huangapple go评论60阅读模式
英文:

Callback function to move the object down not rendering right

问题

我一直在尝试在我的项目中进行一些额外的工程工作,作为我的JavaScript实验的一部分。我设法让我的"防护罩"向左和向右移动,但不能向下移动。当我调用事件来使它向下移动(moveDodgerBottom)时,它不按要求执行,而是向上移动。有人可以帮助我检查我的代码吗?

const dodger = document.getElementById("dodger")

dodger.style.backgroundColor = '#FF69B4'

function moveDodgerLeft() {
    const leftNumbers = dodger.style.left.replace("px", "");
    const left = parseInt(leftNumbers, 10);

    if (left > 0) {
        dodger.style.left = `${left - 1}px`;
    }
}

function moveDodgerRight() {
    const rightNumbers = dodger.style.left.replace("px", "");
    const right = parseInt(rightNumbers, 10);

    if (right < 360) {
        dodger.style.left = `${right + 1}px`;
    }
}

function moveDodgerBottom() {
    const bottomNumbers = dodger.style.bottom.replace("px", "");
    const bottom = parseInt(bottomNumbers, 10);

    if (bottom < 360) {
        dodger.style.bottom = `${bottom + 1}px`;
    }
}

//这个函数 - moveDodgerBottom() 让"防护罩"向上移动,而不是向下移动

function moveDodgerTop() {
    const topNumbers = dodger.style.top.replace("px", "");
    const top = parseInt(topNumbers, 10);
    if (top < 0) {
        dodger.style.bottom = `${bottom - 1}px`;
    }
}

//这个函数 moveDodgerTop 似乎不起作用

document.addEventListener("keydown", function(event) {
    if (event.key === "ArrowLeft") {
        moveDodgerLeft();
    } else if (event.key === "ArrowRight") {
        moveDodgerRight();
    } else if (event.key === "ArrowUp") {
        moveDodgerTop();
    } else if (event.key === "ArrowDown") {
        moveDodgerBottom();
    }
});

我尝试使用相同的技巧,使"防护罩"向左移动(通过${left - 1})和向右移动(通过${left + 1})。我明白,对于底部,"防护罩"需要${bottom + 1}的输入,所以我认为"防护罩"需要${bottom - 1}来使其向上移动。

英文:

I have been trying to do some extra engineering work on my project as part of my javascript lab.
I managed to get my dodger to move to the left and right but not to the bottom. Also when I invoke the event to make it go down (moveDodgerBottom), it doesn't do as asked and instead moves it up.
Can someone please help me with my code?

const dodger = document.getElementById(&quot;dodger&quot;)
dodger.style.backgroundColor = &#39;#FF69B4&#39;
function moveDodgerLeft() {
const leftNumbers = dodger.style.left.replace(&quot;px&quot;, &quot;&quot;); //remember, the dodger styles left, right, etc are in relation to the parent object that it is placed within. 
const left = parseInt(leftNumbers, 10);
if (left &gt; 0) {
dodger.style.left = `${left - 1}px`;
}
}
function moveDodgerRight() {
const rightNumbers = dodger.style.left.replace(&quot;px&quot;, &quot;&quot;);
const right = parseInt(rightNumbers, 10);
if (right &lt; 360) {
dodger.style.left = `${right + 1}px`;
}
}
function moveDodgerBottom() {
const bottomNumbers = dodger.style.bottom.replace(&quot;px&quot;, &quot;&quot;);
const bottom = parseInt(bottomNumbers, 10);
if (bottom &lt; 360) {
dodger.style.bottom = `${bottom + 1}px`;
}
}
//this function - moveDodgerBottom() makes the dodger go up instead of going down
function moveDodgerTop() {
const topNumbers = dodger.style.top.replace(&quot;px&quot;, &quot;&quot;);
const top = parseInt(topNumbers, 10);
if (top &lt; 0) {
dodger.style.bottom = `${bottom - 1}px`;
}
}
//this function moveDodgerTop doesn&#39;t seem to be working
document.addEventListener(&quot;keydown&quot;, function(event) {
if (event.key === &quot;ArrowLeft&quot;) {
moveDodgerLeft();
} else if (event.key === &quot;ArrowRight&quot;) {
moveDodgerRight();
} else if (event.key === &quot;ArrowUp&quot;) {
moveDodgerTop();
} else if (event.key === &quot;ArrowDown&quot;) {
moveDodgerBottom();
}
});

I tried using the technique before to get the dodger to move to the left by changing the ${left - 1} and then to the right by ${left +1}. I understand that with the bottom, the dodger needs an input of ${bottom +1} so I was thinking that the dodger would require a ${bottom - 1} to make it go up.

答案1

得分: 1

根据您创建的四个函数的模式,也许您可以更改:

function moveDodgerTop() {
    const topNumbers = dodger.style.top.replace("px", "");
    const top = parseInt(topNumbers, 10);
    if (top < 0) {
        dodger.style.bottom = `${bottom - 1}px`;
    }
}

为:

function moveDodgerTop() {
    const topNumbers = dodger.style.bottom.replace("px", "");
    const top = parseInt(topNumbers, 10);
    if (top > 0) {
        dodger.style.bottom = `${top - 1}px`;
    }
}

其他方法:

不要使用 style.leftstyle.rightstyle.topstyle.bottom,也许使用 style.transform 是正确的选项来移动 div。在 style.transform 中填入 translate(x, y),其中值 x 表示 X 轴的坐标,值 y 表示 Y 轴的坐标。两个值都可以使用像素(px)填写。

const dodger = document.getElementById("dodger")

dodger.style.backgroundColor = '#FF69B4';

let position = {
  x: 0,
  y: 0
}

function moveDodgerLeft() {
    position.x = position.x - 1;
}

function moveDodgerRight() {
    position.x = position.x + 1;
}

function moveDodgerBottom() {
    position.y = position.y + 1;
}

function moveDodgerTop() {
    position.y = position.y - 1;
}

document.addEventListener("keydown", function(event) {
    if (event.key === "ArrowLeft") {
        moveDodgerLeft();
    } else if (event.key === "ArrowRight") {
        moveDodgerRight();
    } else if (event.key === "ArrowUp") {
        moveDodgerTop();
    } else if (event.key === "ArrowDown") {
        moveDodgerBottom();
    }
    dodger.style.transform = `translate(${position.x}px,${position.y}px)`;
});
#dodger {
  width: 200px;
  height: 200px;
}
<div id="dodger"></div>
英文:

Based on the pattern of the four functions you created, maybe you can change :

function moveDodgerTop() {
const topNumbers = dodger.style.top.replace(&quot;px&quot;, &quot;&quot;);
const top = parseInt(topNumbers, 10);
if (top &lt; 0) {
dodger.style.bottom = `${bottom - 1}px`;
}
}

with :

function moveDodgerTop() {
const topNumbers = dodger.style.bottom.replace(&quot;px&quot;, &quot;&quot;);
const top = parseInt(topNumbers, 10);
if (top &gt; 0) {
dodger.style.bottom = `${top - 1}px`;
}
}

Other method:

Instead of using style.left, style.right, style.top, or style.bottom, maybe style.transform is the right option to make the div move. Fill in style.transform with translate(x,y) where the value x represents the coordinates of the X axis and the value y represents the coordinates of the Y axis. Both values can be filled in pixels (px).

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const dodger = document.getElementById(&quot;dodger&quot;)
dodger.style.backgroundColor = &#39;#FF69B4&#39;;
let position = {
x: 0,
y: 0
}
function moveDodgerLeft() {
position.x = position.x - 1;
}
function moveDodgerRight() {
position.x = position.x + 1;
}
function moveDodgerBottom() {
position.y = position.y + 1;
}
function moveDodgerTop() {
position.y = position.y - 1;
}
document.addEventListener(&quot;keydown&quot;, function(event) {
if (event.key === &quot;ArrowLeft&quot;) {
moveDodgerLeft();
} else if (event.key === &quot;ArrowRight&quot;) {
moveDodgerRight();
} else if (event.key === &quot;ArrowUp&quot;) {
moveDodgerTop();
} else if (event.key === &quot;ArrowDown&quot;) {
moveDodgerBottom();
}
dodger.style.transform = `translate(${position.x}px,${position.y}px`;
});

<!-- language: lang-css -->

#dodger {
width: 200px;
height: 200px;
}

<!-- language: lang-html -->

&lt;div id=&quot;dodger&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月27日 10:17:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定