0

我正在 AppLab 中制作童话故事。我使用了一个我称之为“故事”的数据库,其中包含故事事件以及游戏是否开始(我还没有制作游戏)。无论如何,我必须readRecords()从数据库中获取事件,并在回调函数中创建一个 textLabel(我无法创建 textAreas,但如果我错了请纠正我)。对于每个 textLabel,应该有一个唯一的 ID "event"+scrN:。如果我运行我的整个函数一次,一切都会按预期运行,但如果我运行两次,它会显示WARNING: Line: 10: The textLabel() id parameter refers to an id ("event2") which already exists.,尽管第一个元素应该是“event1”。我把console.log(textID)它打印了两次“event2”。如果你不明白我的意思,这里是代码:

var scr = "screen";
var scrN = 0;
var maxWidth = 320;
var maxHeight = 450;
function create(widType, id, text, x, y, width, height) {
  if (widType === "button") {
    button(id, text);
  }
  else if (widType === "text") {
    textLabel(id, text);
  }
  setProperty(id, "x", x);
  setProperty(id, "y", y);
  setProperty(id, "width", width);
  setProperty(id, "height", height);
  if (widType === "text") {
    setProperty(id, "background-color", rgb(0,0,0,0));
    setProperty(id, "border-color", rgb(0,0,0,0));
    setProperty(id, "text-color", rgb(0,0,0));
    setProperty(id, "font-size", 17.5);
  }
  else if (widType === "button") {
    setProperty(id, "text-color", rgb(0,0,0));
    setProperty(id, "font-size", 16);
  }
}

function screenChange(dir) {
  if (dir === "NEXT") {
    scrN++;
  }
  else if (dir === "BACK") {
    scrN--;
  }
  if (dir === "NEXT") {
    setScreen(scr+scrN);
    var buttonWidth = 80;
    var buttonHeight = 40;
    var next = "Next";
    var nextID = next.toLowerCase()+scrN;
    var nextx = 220;
    var nexty = 400;
    create("button", nextID, next,
    nextx, nexty, buttonWidth, buttonHeight);
    var back = "Back";
    var backID = back.toLowerCase()+scrN;
    var backx = -80;
    var backy = nexty;
    create("button", backID, back,
    backx, backy, buttonWidth, buttonHeight);
    readRecords("story", {}, function(records) {
      var textID = "event"+scrN;
      console.log(textID);
      create("text", textID, records[scrN-1].event,
      0, -50, maxWidth, 90);
    });
  }
}
screenChange("NEXT");
screenChange("NEXT");

我需要帮助!readRecords 函数是否更改了 main 函数过去调用的 textID 变量?如果是这样,我该怎么办?谢谢

4

1 回答 1

0

您可以在设计选项卡中创建文本区域。您还可以使用setParent()块将每个元素作为文本区域的父级。

setParent(id, parentId);

要清除文本区域并添加新元素,只需将文本区域的文本设置为“”。

于 2021-11-10T04:45:57.513 回答