为“虚拟宠物”游戏添加了功能,但我找不到我的错误
Posted
技术标签:
【中文标题】为“虚拟宠物”游戏添加了功能,但我找不到我的错误【英文标题】:Added features to "Virtual pet" game but I can't find my error 【发布时间】:2019-03-11 14:52:51 【问题描述】:我刚刚为我的虚拟宠物添加了睡眠和饥饿功能,现在脚本无法运行,我不确定我做错了什么,因为一切看起来都很好。我将向您展示原始脚本(有效)和我修改后的脚本。
我不太确定我错过了什么,我花了相当多的时间来寻找我的错误,任何帮助将不胜感激!
原文:
<!-- Developed @ the University of Advancing Technology for use in UAT courses. -->
<html>
<head>
<title>Virtual Pet</title>
<script>
// Virtual Pet Stats and starting values
// TODO: Change the name of your Virtual Pet on the line below.
var petName = "Virtual Pet";
var currentHealth = 60;
var maxHealth = 100;
var petStatus = "healthy";
// TODO: Add more Virtual Pet stats that will be modified by your functions
// Increases the current health of the Virtual Pet until it is maxed out.
function exercise()
currentHealth = currentHealth + 5;
if(currentHealth > maxHealth)
currentHealth = maxHealth;
// Update the display after an action has occurred.
updateDisplay();
// TODO: Write new functions that will process and modify the new stats you have created. (The majority of your new code should go here.)
// Modifies any stats that automatically change every few seconds.
// (For example, health decreases every few seconds so the play needs to occationally exercise their Virtual Pet)
function changesOverTime()
currentHealth = currentHealth - 4;
// TODO: Add in other changes to the Virtual Pet stats that occur on a regular basis.
// Checks the pet's health and modifies the status accordingly
function checkHealth()
if(currentHealth <= 0 )
petStatus = "Dead";
currentHealth = 0;
//TODO (Optional): Add other health status checks (For example, sick if health < 15)
// Displays a Title to the screen for your Virtual Pet game
function displayTitle()
// TODO (Optional): Create your own title
// Displays the current pet stats to the screen.
function displayPetStats()
document.write("<h2>" + petName + " Status: " + petStatus + "</h2>");
document.write("<p>Health = " + currentHealth + " Max Health = " + maxHealth + "</p>");
// TODO: Add the display of new Virtual Pet stats here
document.close();
// Displays the buttons to the screen enabling the user to interact with their virtual pet.
function displayUserOptions()
if(petStatus != "Dead")
document.write("<button onclick='exercise()'>Exercise</button>");
// TODO: Create buttons for other actions
// Calls all the functions that display information to the screen.
function updateDisplay()
displayTitle();
displayUserOptions();
displayPetStats();
// This function executes the game and manages the passing of time.
function gameLoop(timestamp)
if(timestamp > last_iteration + time_interval)
last_iteration = timestamp;
changesOverTime();
checkHealth();
// TODO: Check other Virtual Pet stats and update the petStatus accordingly
// After all stats updates are done, update/recreate the display
updateDisplay();
// Life continues unless the Virtual Pet is dead (health <= 0)
if(petStatus != "Dead")
// Executes the gameLoop function once again.
requestAnimationFrame(gameLoop);
// Other global variables that control the timing of the game.
var time_interval = 5000;
var last_iteration = 0;
</script>
</head>
<body>
<h1>My Virtual Pet!</h1>
<!-- Initiate the gameLoop for the first time.-->
<button onclick='gameLoop()'>Bring my Virtual Pet to Life!</button>
</body>
</html>
我修改后的脚本:
<!-- Developed @ the University of Advancing Technology for use in UAT courses. -->
<html>
<head>
<title>Virtual Pet</title>
<script>
// Virtual Pet Stats and starting values
// TODO: Change the name of your Virtual Pet on the line below.
var petName = "Virtual Pet";
var currentHealth = 60;
var maxHealth = 100;
var petStatus = "healthy";
var tiredness = 60;
var maxTiredness = 100;
var hunger = 60;
var maxHunger = 100;
// TODO: Add more Virtual Pet stats that will be modified by your functions
// Increases the current health of the Virtual Pet until it is maxed out.
function exercise()
currentHealth = currentHealth + 5;
if(currentHealth > maxHealth)
currentHealth = maxHealth;
// Update the display after an action has occurred.
updateDisplay();
function sleep()
var sleepamount = prompt("How many minutes would you like to sleep?");
tiredness + sleepamount = tiredness;
if tiredness >= 100
tiredness = 100;
// Update the display after an action has occurred.
updateDisplay();
function checkHunger()
var feed = prompt("How many snacks do you want to eat?");
hunger + feed = hunger;
if hunger >= 100
hunger = 100;
// Update the display after an action has occurred.
updateDisplay();
// TODO: Write new functions that will process and modify the new stats you have created. (The majority of your new code should go here.)
// Modifies any stats that automatically change every few seconds.
// (For example, health decreases every few seconds so the play needs to occationally exercise their Virtual Pet)
function changesOverTime()
currentHealth = currentHealth - 4;
tiredness = tiredness - 4;
hunger = hunger - 4;
// TODO: Add in other changes to the Virtual Pet stats that occur on a regular basis.
// Checks the pet's health and modifies the status accordingly
function checkHealth()
if(currentHealth <= 0 )
petStatus = "Dead";
currentHealth = 0;
//TODO (Optional): Add other health status checks (For example, sick if health < 15)
function checkStarved()
if(hunger <= 0 )
petStatus = "Dead";
hunger = 0;
else if hunger >= 30 && hunger <= 75
petStatus = "Hungry";
//TODO (Optional): Add other health status checks (For example, sick if health < 15)
// Displays a Title to the screen for your Virtual Pet game
function displayTitle()
// TODO (Optional): Create your own title
// Displays the current pet stats to the screen.
function displayPetStats()
document.write("<h2>" + petName + " Status: " + petStatus + "</h2>");
document.write("<p>Health = " + currentHealth + " Max Health = " + maxHealth + "</p>");
document.write("<p>Hunger = " + hunger + " Max Hunger = " + maxHunger + "</p>");
document.write("<p>Tiredness = " + tiredness + " Max Tiredness = " + maxTiredness + "</p>");
// TODO: Add the display of new Virtual Pet stats here
document.close();
// Displays the buttons to the screen enabling the user to interact with their virtual pet.
function displayUserOptions()
if(petStatus != "Dead")
document.write("<button onclick='exercise()'>Exercise</button>");
document.write("<button onclick='sleep()'>Sleep</button>");
document.write("<button onclick='checkHunger()'>Feed</button>");
// TODO: Create buttons for other actions
// Calls all the functions that display information to the screen.
function updateDisplay()
displayTitle();
displayUserOptions();
displayPetStats();
// This function executes the game and manages the passing of time.
function gameLoop(timestamp)
if(timestamp > last_iteration + time_interval)
last_iteration = timestamp;
changesOverTime();
checkHealth();
checkStarved()
// TODO: Check other Virtual Pet stats and update the petStatus accordingly
// After all stats updates are done, update/recreate the display
updateDisplay();
// Life continues unless the Virtual Pet is dead (health <= 0)
if(petStatus != "Dead")
// Executes the gameLoop function once again.
requestAnimationFrame(gameLoop);
// Other global variables that control the timing of the game.
var time_interval = 5000;
var last_iteration = 0;
</script>
</head>
<body>
<h1>My Virtual Pet!</h1>
<!-- Initiate the gameLoop for the first time.-->
<button onclick='gameLoop()'>Bring my Virtual Pet to Life!</button>
</body>
</html>
【问题讨论】:
此hunger + feed = hunger;
无效。你肯定是说hunger += feed;
很好,我确实改变了饥饿+饲料=饥饿喂+=饥饿
我也将疲劳度 + sleepamount = 疲劳度更改为 sleepamount += 疲劳度,但它仍然无法运行
【参考方案1】:
您多次出现两个错误。
分配变量声明需要在 = 符号的左侧,疲倦 + 饥饿 = 饥饿需要是饥饿 = 疲倦 + 饥饿
if语句缺失(),如果饥饿>0需要为if(饥饿>0)
这是一个更新的脚本。
<!-- Developed @ the University of Advancing Technology for use in UAT courses. -->
<html>
<head>
<title>Virtual Pet</title>
<script>
// Virtual Pet Stats and starting values
// TODO: Change the name of your Virtual Pet on the line below.
var petName = "Virtual Pet";
var currentHealth = 60;
var maxHealth = 100;
var petStatus = "healthy";
var tiredness = 60;
var maxTiredness = 100;
var hunger = 60;
var maxHunger = 100;
// TODO: Add more Virtual Pet stats that will be modified by your functions
// Increases the current health of the Virtual Pet until it is maxed out.
function exercise()
currentHealth = currentHealth + 5;
if(currentHealth > maxHealth)
currentHealth = maxHealth;
// Update the display after an action has occurred.
updateDisplay();
function sleep()
var sleepamount = prompt("How many minutes would you like to sleep?");
tiredness = sleepamount + tiredness;
if (tiredness >= 100)
tiredness = 100;
// Update the display after an action has occurred.
updateDisplay();
function checkHunger()
var feed = prompt("How many snacks do you want to eat?");
hunger = feed + hunger;
if (hunger >= 100)
hunger = 100;
// Update the display after an action has occurred.
updateDisplay();
// TODO: Write new functions that will process and modify the new stats you have created. (The majority of your new code should go here.)
// Modifies any stats that automatically change every few seconds.
// (For example, health decreases every few seconds so the play needs to occationally exercise their Virtual Pet)
function changesOverTime()
currentHealth = currentHealth - 4;
tiredness = tiredness - 4;
hunger = hunger - 4;
// TODO: Add in other changes to the Virtual Pet stats that occur on a regular basis.
// Checks the pet's health and modifies the status accordingly
function checkHealth()
if(currentHealth <= 0 )
petStatus = "Dead";
currentHealth = 0;
//TODO (Optional): Add other health status checks (For example, sick if health < 15)
function checkStarved()
if(hunger <= 0 )
petStatus = "Dead";
hunger = 0;
else if (hunger >= 30 && hunger <= 75)
petStatus = "Hungry";
//TODO (Optional): Add other health status checks (For example, sick if health < 15)
// Displays a Title to the screen for your Virtual Pet game
function displayTitle()
// TODO (Optional): Create your own title
// Displays the current pet stats to the screen.
function displayPetStats()
document.write("<h2>" + petName + " Status: " + petStatus + "</h2>");
document.write("<p>Health = " + currentHealth + " Max Health = " + maxHealth + "</p>");
document.write("<p>Hunger = " + hunger + " Max Hunger = " + maxHunger + "</p>");
document.write("<p>Tiredness = " + tiredness + " Max Tiredness = " + maxTiredness + "</p>");
// TODO: Add the display of new Virtual Pet stats here
document.close();
// Displays the buttons to the screen enabling the user to interact with their virtual pet.
function displayUserOptions()
if(petStatus != "Dead")
document.write("<button onclick='exercise()'>Exercise</button>");
document.write("<button onclick='sleep()'>Sleep</button>");
document.write("<button onclick='checkHunger()'>Feed</button>");
// TODO: Create buttons for other actions
// Calls all the functions that display information to the screen.
function updateDisplay()
displayTitle();
displayUserOptions();
displayPetStats();
// This function executes the game and manages the passing of time.
function gameLoop(timestamp)
if(timestamp > last_iteration + time_interval)
last_iteration = timestamp;
changesOverTime();
checkHealth();
checkStarved()
// TODO: Check other Virtual Pet stats and update the petStatus accordingly
// After all stats updates are done, update/recreate the display
updateDisplay();
// Life continues unless the Virtual Pet is dead (health <= 0)
if(petStatus != "Dead")
// Executes the gameLoop function once again.
requestAnimationFrame(gameLoop);
// Other global variables that control the timing of the game.
var time_interval = 5000;
var last_iteration = 0;
</script>
</head>
<body>
<h1>My Virtual Pet!</h1>
<!-- Initiate the gameLoop for the first time.-->
<button onclick='gameLoop()'>Bring my Virtual Pet to Life!</button>
</body>
</html>
【讨论】:
谢谢你,我很高兴得知变量需要在左侧,这就是破坏我的代码的原因!也感谢您对我的错误的其他回答。【参考方案2】:代码中有两处错误:
=
的左侧必须是变量。您正在使用 tiredness + sleepamount = tiredness
这是一些地方。将其更改为tiredness = tiredness + sleepamount
if
的条件应该在()
中,但您使用的是这样的hunger >= 100
<!-- Developed @ the University of Advancing Technology for use in UAT courses. -->
<html>
<head>
<title>Virtual Pet</title>
<script>
// Virtual Pet Stats and starting values
// TODO: Change the name of your Virtual Pet on the line below.
var petName = "Virtual Pet";
var currentHealth = 60;
var maxHealth = 100;
var petStatus = "healthy";
var tiredness = 60;
var maxTiredness = 100;
var hunger = 60;
var maxHunger = 100;
// TODO: Add more Virtual Pet stats that will be modified by your functions
// Increases the current health of the Virtual Pet until it is maxed out.
function exercise()
currentHealth = currentHealth + 5;
if(currentHealth > maxHealth)
currentHealth = maxHealth;
// Update the display after an action has occurred.
updateDisplay();
function sleep()
var sleepamount = prompt("How many minutes would you like to sleep?");
tiredness = tiredness + sleepamount;
if (tiredness >= 100)
tiredness = 100;
// Update the display after an action has occurred.
updateDisplay();
function checkHunger()
var feed = prompt("How many snacks do you want to eat?");
hunger = hunger + feed;
if (hunger >= 100)
hunger = 100;
// Update the display after an action has occurred.
updateDisplay();
// TODO: Write new functions that will process and modify the new stats you have created. (The majority of your new code should go here.)
// Modifies any stats that automatically change every few seconds.
// (For example, health decreases every few seconds so the play needs to occationally exercise their Virtual Pet)
function changesOverTime()
currentHealth = currentHealth - 4;
tiredness = tiredness - 4;
hunger = hunger - 4;
// TODO: Add in other changes to the Virtual Pet stats that occur on a regular basis.
// Checks the pet's health and modifies the status accordingly
function checkHealth()
if(currentHealth <= 0 )
petStatus = "Dead";
currentHealth = 0;
//TODO (Optional): Add other health status checks (For example, sick if health < 15)
function checkStarved()
if(hunger <= 0 )
petStatus = "Dead";
hunger = 0;
else if (hunger >= 30 && hunger <= 75)
petStatus = "Hungry";
//TODO (Optional): Add other health status checks (For example, sick if health < 15)
// Displays a Title to the screen for your Virtual Pet game
function displayTitle()
// TODO (Optional): Create your own title
// Displays the current pet stats to the screen.
function displayPetStats()
document.write("<h2>" + petName + " Status: " + petStatus + "</h2>");
document.write("<p>Health = " + currentHealth + " Max Health = " + maxHealth + "</p>");
document.write("<p>Hunger = " + hunger + " Max Hunger = " + maxHunger + "</p>");
document.write("<p>Tiredness = " + tiredness + " Max Tiredness = " + maxTiredness + "</p>");
// TODO: Add the display of new Virtual Pet stats here
document.close();
// Displays the buttons to the screen enabling the user to interact with their virtual pet.
function displayUserOptions()
if(petStatus != "Dead")
document.write("<button onclick='exercise()'>Exercise</button>");
document.write("<button onclick='sleep()'>Sleep</button>");
document.write("<button onclick='checkHunger()'>Feed</button>");
// TODO: Create buttons for other actions
// Calls all the functions that display information to the screen.
function updateDisplay()
displayTitle();
displayUserOptions();
displayPetStats();
// This function executes the game and manages the passing of time.
function gameLoop(timestamp)
if(timestamp > last_iteration + time_interval)
last_iteration = timestamp;
changesOverTime();
checkHealth();
checkStarved()
// TODO: Check other Virtual Pet stats and update the petStatus accordingly
// After all stats updates are done, update/recreate the display
updateDisplay();
// Life continues unless the Virtual Pet is dead (health <= 0)
if(petStatus != "Dead")
// Executes the gameLoop function once again.
requestAnimationFrame(gameLoop);
// Other global variables that control the timing of the game.
var time_interval = 5000;
var last_iteration = 0;
</script>
</head>
<body>
<h1>My Virtual Pet!</h1>
<!-- Initiate the gameLoop for the first time.-->
<button onclick='gameLoop()'>Bring my Virtual Pet to Life!</button>
</body>
</html>
【讨论】:
谢谢,我很欣赏关于将变量放在 = 符号左侧以及何时使用 () 的重要课程!以上是关于为“虚拟宠物”游戏添加了功能,但我找不到我的错误的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Unity 中添加 Google Play 游戏注册脚本