按钮名称不变
Name of button not changing
我正在尝试制作一款飞鸟游戏,您可以在其中选择 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
不清楚您的处理方式 "put 1 into difficulty"。正如所写,这不会做任何事情。如果我明白你想做什么,这个值需要存储在一个变量中,如果默认值总是以“1”开头,你可以这样做:
本地难度=1
此外,您的 changeDiff 处理程序应该使用 "else" 语句,或者考虑在设置一个值后退出您的处理程序,以避免一个 "if" 语句为另一个语句设置一个 true 条件——因为它代表,您的代码只会在前两个 "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
如果你想提高效率(更少的代码行),你可以使用单个 "set label" 语句并从一组标签项中获取标签名称(难度值为 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.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
我正在尝试制作一款飞鸟游戏,您可以在其中选择 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
不清楚您的处理方式 "put 1 into difficulty"。正如所写,这不会做任何事情。如果我明白你想做什么,这个值需要存储在一个变量中,如果默认值总是以“1”开头,你可以这样做:
本地难度=1
此外,您的 changeDiff 处理程序应该使用 "else" 语句,或者考虑在设置一个值后退出您的处理程序,以避免一个 "if" 语句为另一个语句设置一个 true 条件——因为它代表,您的代码只会在前两个 "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
如果你想提高效率(更少的代码行),你可以使用单个 "set label" 语句并从一组标签项中获取标签名称(难度值为 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.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