让角色按照我给它的命令行动

问题描述 投票:0回答:1

当我在编码并试图弄清楚该怎么做时,我试图让一个角色移动到按钮命令。 (上下左右)所以我做了我认为可行的事情但没有,所以我尝试调试它。 (当我调试某些东西时,我经常 // 一切然后取消// 一个部分并尝试调试该部分并在所有其他部分上使用该调试我不会 // 这次但它会有所不同(我想要颜色)) .语言是javascript。这就是我所拥有的。

onEvent("up", "click", function( ) {
  var currentY = getProperty("player", "y");
  console.log("currentY = ", currentY + 10);
});

onEvent("down", "click", function( ) {
  console.log("inside of onEvent - down");
  setProperty("player", "y", +3);
});

onEvent("left", "click", function( ) {
  console.log("inside of onEvent - left");
  setProperty("player", "x", -3);
});
 onEvent("right", "click", function( ) {   
 console.log("inside of onEvent - right");
  setProperty("player", "x", +3);
});
javascript user-controls
1个回答
0
投票

我注意到您的代码有几个问题可能会导致问题。 试试这个:

onEvent("up", "click", function() {
  var currentY = getProperty("player", "y");
  // You need to update the "y" property of the "player" element by subtracting a value to move it up.
  setProperty("player", "y", currentY - 10);
});

onEvent("down", "click", function() {
  var currentY = getProperty("player", "y");
  // You need to update the "y" property of the "player" element by adding a value to move it down.
  setProperty("player", "y", currentY + 10);
});

onEvent("left", "click", function() {
  var currentX = getProperty("player", "x");
  // You need to update the "x" property of the "player" element by subtracting a value to move it left.
  setProperty("player", "x", currentX - 10);
});

onEvent("right", "click", function() {
  var currentX = getProperty("player", "x");
  // You need to update the "x" property of the "player" element by adding a value to move it right.
  setProperty("player", "x", currentX + 10);
});

我改变了什么:

对于所有按钮事件,我在更新之前使用 getProperty() 获取了当前位置。 在“向上”按钮事件中,我将“y”属性设置为 currentY - 10 而不是仅记录值。这实际上会使角色向上移动。 在其他按钮事件中,我已经更正了“x”和“y”属性的值。

确保您的“player”元素最初设置了“x”和“y”属性。如果它是一个 HTML 元素,你可能需要使用像“top”和“left”这样的 CSS 属性来代替“x”和“y”。

© www.soinside.com 2019 - 2024. All rights reserved.