我正在尝试制作一个飞扬的鸟游戏,您可以从3种不同的困难中进行选择。但是按钮的标签不会改变。现在,我有一个应该改变难度的功能。这是代码
我尝试更改代码的大写形式,尽管这样做不起作用。
这是难度按钮的代码:
on mouseUp
changeDiff
end mouseUp
这是卡上的代码:
put 1 into difficulty
on changeDiff
if difficulty = 2 then
put 1 into difficulty
set the Label of btn "Difficulty" to "normal"
end if
if difficulty = 1.5 then
put 2 into difficulty
set the Label of btn "Difficulty" to "DEMON"
end if
if difficulty = 1 then
put 1.5 into difficulty
set the Label of btn "Difficulty" to "hard"
end if
end changeDiff
答案 0 :(得分:2)
目前尚不清楚您如何处理“让1陷入困境”。如所写,这不会做任何事情。如果我了解您要执行的操作,则此值需要存储在变量中,并且如果默认值始终以“ 1”开头,则可以执行以下操作:
本地难度= 1
此外,您的changeDiff处理程序应使用“ else”语句,或考虑在设置值后退出处理程序,以避免一个“ if”语句为另一个设置真实条件-就目前而言,您的代码只会在前两个“ if”选项之间切换。您可以这样编写处理程序:
local difficulty = 1
on changeDiff
if difficulty = 2 then
put 1 into difficulty
set the label of btn "Difficulty" to "normal"
else
if difficulty = 1.5 then
put 2 into difficulty
set the label of btn "Difficulty" to "DEMON"
else
if difficulty = 1 then
put 1.5 into difficulty
set the label of btn "Difficulty" to "hard"
end if
end if
end if
end changeDiff
您可以考虑使用如下所示的switch语句(更易于阅读):
local difficulty = 1
on changeDiff
switch difficulty
case 2
put 1 into difficulty
set the label of btn "Difficulty" to "normal"
break
case 1.5
put 2 into difficulty
set the label of btn "Difficulty" to "DEMON"
break
case 1
put 1.5 into difficulty
set the label of btn "Difficulty" to "hard"
end switch
end changeDiff
如果您想提高效率(减少代码行),则可以使用单个“设置标签”语句,并从一组标签项(难度值为1、2、3)中获取标签名称:>
local difficulty = 1
on changeDiff
add 1 to difficulty
if difficulty > 3 then put 1 into difficulty
set the label of btn "Difficulty" to item difficulty of "Normal,Hard,Demon"
end changeDiff
希望这会有所帮助。
答案 1 :(得分:1)
考虑这种方法:
使用带有选项1、1.5和2的选项菜单按钮选择难度级别。像这样编写选项菜单脚本,将所选的选项作为参数传递:
on menuPick pItemName
changeDiff pItemName
end menuPick
然后在您的卡片脚本中:
local difficulty
on changeDiff pDiff
switch pDiff
case 2
put 1 into difficulty
set the label of btn "Difficulty" to "normal"
break
case 1.5
put 2 into difficulty
set the label of btn "Difficulty" to "DEMON"
break
case 1
put 1.5 into difficulty
set the label of btn "Difficulty" to "hard"
break
end switch
end changeDiff