【问题标题】:Call function not working?呼叫功能不起作用?
【发布时间】:2016-03-27 23:59:38
【问题描述】:

我正在批量制作一个选择你自己的冒险游戏,我想在其中包含统计数据以跟踪你在游戏中的表现。我想显示统计信息,但每次我有一个cls 命令时都必须这样做,因为我会经常使用那个命令,所以我做了一个名为:Stats 的块,所以我可以在每次需要时调用它.它工作正常,但统计显示后的叙述似乎不再起作用。我想保留:colorEcho 块,以便玩家可以区分对话、叙述和命令以输入选择。有关如何解决此问题的任何想法?

:Start
::Game goes here.
cls
Echo Please enter your player name.
set /p PlayerName="Player name: "
cls
call :Stats
call :colorEcho 7 "You lay on the cold, hard ground, sleeping away. It's   been 1 month since the Virus spread..."
Echo.
call :colorEcho 7 "The sun rises over the horizon, warming up your dirt-caked body."
Echo . 
pause
exit

:Stats
call :colorEcho A "Current Health = "
call :colorEcho C " %Health%"
Echo.
call :colorEcho A "Hunger = "
call :colorEcho C " %Food%"
Echo.
call :colorEcho A "Thirst = "
call :colorEcho C " %Water%"
Echo.
call :colorEcho A "Infection = "
call :colorEcho C " %Infection%"
Echo. 
call :colorEcho A "Stamina = "
call :colorEcho C " %Stamina%"
Echo.
Echo.
pause

:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

旁注:我已经定义了所有变量。这是我确定的。

应该发生什么:

实际发生的情况:

【问题讨论】:

  • 您在子程序结束时忘记了exit /bgoto :eof,所以:Start 属于:colorEcho
  • 这到底是做什么的?

标签: function batch-file call cls


【解决方案1】:

你忘了回拨到另一个区块

@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
  set "DEL=%%a"
)
title Ace
set Health=100
set Food=100
set Water=100
set Infection=100
set Stamina=100
color A
Echo Welcome, %USERNAME% to Ace, a crudely made Choose-Your-Own-Adventure game. You must complete 

this in one try. 
call :colorEcho A "There is" 
call :colorEcho C " no saving" 
Echo . Are you ready?
Echo Your options are:
call :colorEcho C "================="
Echo.
Echo 1. Yes
Echo 2. No
call :colorEcho C "================="
Echo.
Echo Please enter the number corresponding to your answer.
set /p Answer1="Enter your choice: "
if "%Answer1%" == "1" goto Start
if "%Answer1%" == "2" goto Quit

:Quit
Echo You have chosen to quit.
pause
exit

:Start
::Game goes here.
cls
Echo Please enter your player name.
set /p PlayerName="Player name: "
cls
call :Stats

Echo.
call :colorEcho C "You lay on the cold, hard ground, sleeping away. It's been 1 month since the Virus 

spread"
Echo.
call :colorEcho C "The sun rises over the horizon, warming up your dirt-caked body."
Echo. 
pause
exit

:Stats
call :colorEcho A "Current Health = "
call :colorEcho C " %Health%"
Echo.
call :colorEcho A "Hunger = "
call :colorEcho C " %Food%"
Echo.
call :colorEcho A "Thirst = "
call :colorEcho C " %Water%"
Echo.
call :colorEcho A "Infection = "
call :colorEcho C " %Infection%"
Echo. 
call :colorEcho A "Stamina = "
call :colorEcho C " %Stamina%"
EXIT /B
Echo.
Echo.

:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

【讨论】:

  • 您认为:derp 标签的目的是什么?如果你尝试像这样返回主程序,你可能会超过嵌套调用的限制......
  • 需要 derp 标签
  • 不,绝对不是!您所要做的就是插入exit /Bgoto :EOF before :colorecho,正如this comment 中所建议的那样。
  • 我知道你可以做到。我的意思是这样的事情是必要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
相关资源
最近更新 更多