【发布时间】:2016-07-16 04:03:56
【问题描述】:
我正在制作一款游戏,我有一个Health 变量来跟踪玩家在玩游戏时的生命值。有一次,玩家在罐头上咬了一口(我知道这很正常),损失了 2 点生命值。我正在尝试使用算术从“健康”变量中减去 2 HP(此时仍为 100),然后显示他们还剩下多少健康,但在我尝试删除 2 后它仍然显示 100 HP生命值。我做得对吗?
:Beans
call :colorEcho 7 "You decide you're going to have Canned Beans for breakfast. Yum. You grab a knife from the kitchen counter and work the top off. You eat the beans straight from the can. Ouch! You cut your lip! It starts to bleed."
Echo .
set /a "_Health=_Health-2"
Echo.
call :colorEcho C "# ALERT; You have lost 2 Health Points! You are now at %Health% HP!"
Echo.
pause
exit
会发生什么:
【问题讨论】:
-
你试过没有
"s吗? -
我猜这个代码片段在括号
()内的一个块中;如果我是对的,你需要delayed expansion,或者你写set /A _Health-=2... -
@i_am_jorf 是的,我尝试不使用
"s,但得到相同的结果。 -
@aschipfl 这个块不在括号内,但是我尝试了`set /a _Health-=2,但还是不行。
-
当您的变量命名为
_Health时,为什么要扩展%Health%?注意 - 您可以简化为set /a _Health-=2
标签: batch-file math arithmetic-expressions integer-arithmetic