【问题标题】:How to create a function for a hint button in code.org如何在 code.org 中为提示按钮创建函数
【发布时间】:2021-07-28 07:16:57
【问题描述】:

如何在code.org 中为我的刽子手游戏创建函数?

我需要制作一个“提示”按钮,这样当我按下按钮时,指向您要猜测的单词的提示会为您提供线索。

示例

我的话是abandon,当我点击“提示”按钮时,提示如下:

停止支持或照顾(某人);沙漠

我已经为刽子手游戏中的猜词设置了变量,例如:

var words= ["abandon", "above", "about"];
var word = "";

【问题讨论】:

  • 有另一个数组用于定义与words相关的索引
  • 看不到更多代码,很难回答或提出解决方案。请将应用程序/代码的链接或完整代码发布为minimal reproducible example,就像在类似问题中所做的那样:hangman at code.org

标签: javascript function button code.org


【解决方案1】:

我对你的描述做了一个小模型。由于 Code.org 有设计和半码,请注意我的设计。你可以和我的设计互动sample hangman program

我做了一个对应的列表hints。然后我选择一个随机数并存储在index 中。这告诉我要使用wordshints 中的哪个位置。

然后我实现我的三个按钮。第一个,hintBtn,设置标签的文本,并取消隐藏它(它作为 DesignMode 的一部分被隐藏)。

第二个按钮newWordBtn 选择不同的index 作为答案。它还使提示再次隐藏(我们在用户单击hintBtn 时更改提示标签的文本)。

我已将guessBtn 留给您完全实施。

//Code.org Project: https://studio.code.org/projects/applab/38gmbu9k7LCiyPIRALOnHVQgEKVQ1PuPkC7hXmOGc3k
var words= ["abandon", "above", "apple"];
var hints = ["to be left without warning", "not below", "Washington's fruit"];

var index = randomNumber(0, words.length - 1);

var word = words[index];

onEvent("hintBtn", "click", function( ) {
  //this code will run when "Hint" clicked
  //will grab the hint from the hints list 
  //  based on what spot using right now ('index')
  setProperty("hintText", "text", hints[index]);
  //reveal the hint (want to change the text first)
  setProperty("hintText", "hidden", false);
});

onEvent("newWordBtn", "click", function( ) {
  //change which word spot using
  index = randomNumber(0, words.length - 1); 
  //rehide the hint text
  setProperty("hintText", "hidden", true);
});

onEvent("guessBtn", "click", function( ) {
  //to implement
  console.log(word);
});

希望这会有所帮助。

【讨论】:

  • 感谢您的提示,我错过了。我还没有那种能力,因为我没有足够的声望。是时候建立一些声誉了!
  • 更好?我想我已经用正确的格式创建了一个更完整的答案。
  • 很棒:完整的代码,带有解释 cmets,内联添加的说明性图像,一些人类语言作为指导步骤。 ?️ 只有链接似乎坏了,值得一个比“这里”更好的名字。给它一个描述其内容的名称,例如“我的示例刽子手项目”或类似的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2022-06-22
相关资源
最近更新 更多