- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚为我的虚拟宠物添加了 sleep 和饥饿功能,但现在脚本无法运行,我不确定我做错了什么,因为一切看起来都很好。我将向您展示原始脚本(有效)和我修改后的脚本。
我不太确定我错过了什么,我花了相当多的时间来寻找我的错误,我们将不胜感激任何帮助!
原文:
<!-- 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>
最佳答案
您多次遇到两个错误。
赋值变量语句需要在=号的左边,tiredness + hunger = hunger需要是hunger = tiredness + hunger
if 语句缺少 (),if hunger > 0 { } 需要是 if (hunger > 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>
关于javascript - 向 "Virtual pet"游戏添加了功能,但我找不到我的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55104591/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!