javascript - 如何在 code.org 中为提示按钮创建函数
问题描述
如何在code.org中为我的刽子手游戏创建函数?
我需要制作一个“提示”按钮,这样当我按下按钮时,指向您要猜测的单词的提示会为您提供线索。
例子
我的话是abandon
,当我点击“提示”按钮时,线索会说如下:
停止支持或照顾(某人);沙漠
我已经为刽子手游戏中的猜测词设置了变量,例如:
var words= ["abandon", "above", "about"];
var word = "";
解决方案
我对你的描述做了一个小模型。由于 Code.org 有设计和半码,请注意我的设计。您可以与我的设计示例 hangman 程序进行交互。
我做了一个相应的清单hints
。然后我选择一个随机数并存储在index
. 这告诉我使用哪个words
位置hints
。
然后我实现了我的三个按钮。第一个,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);
});
希望这可以帮助。
推荐阅读
- ios - 如何在 Swift 中打开/关闭纵向方向锁定 iPhone
- c# - 带委托的隐式参数修饰符?(C#)
- ios - “代理自动配置 (PAC) 的明文 HTTP URL 方案现已弃用”对 IOS 15 beta 应用程序的影响
- php - 如何根据 Magento 2.4 中的地址计算动态运费
- powerbi - 有没有办法在不使用书签的情况下清除 PBI 报告上的过滤器
- django - 配置 sendgrid 和 django
- reactjs - 反应原生 sdk 41.0.0 过时?
- javascript - 如何以角度向iframe添加关闭按钮?
- c++ - E0028 表达式必须有一个常数值。我想像一个常数一样输入n。谢谢
- reactjs - 使用覆盖部分联合的交集类型