2017-05-29 4 views
-1

Ich bin mir nicht sicher, wie ein neues Gebäude von Array hinzufügen. Ich bin ein Anfänger Javascript Person.Javascript Hinzufügen neuer Gebäude

Ich habe das Speichern/Laden unter anderem zum Backend hinzugefügt, aber die Client-Seite gibt mir Probleme aus irgendeinem Grund.

Ich denke, es hat etwas mit mir nicht unter stehenden Arrays richtig zu tun, aber wenn Sie mich in die richtige Richtung zeigen könnte, würde ich gerne lernen.

I

loadbuilding("taco stand") 

hier genannt ein zweites Gebäude hinzufügen wollen, ist der Code:

var Timer = window.setInterval(function() { 
 
    Tick() 
 
}, 1000); 
 
var buildings = []; 
 

 
//The object declaration for game saves 
 
function GameSave() { 
 
    this.money = 0; 
 
    this.buildings = []; 
 
    for (var i = 0; i < buildings.length; i++) { 
 
     this.buildings[i] = 0; 
 
    } 
 
} 
 

 
//The object declaration for buildings 
 
function Building() { 
 
    this.Name = "Lemonade Stand"; 
 
    this.Cost = 10; 
 
    this.PerSec = 1; 
 
} 
 

 
//The function to initialise all buildings 
 
function InitBuildings() { 
 
    LoadBuilding("Lemonade Stand", 10, 1); 
 
    LoadBuilding("Taco Stand", 100, 1); 
 

 
} 
 

 
//The function to automatically load a building into the buildings array 
 
function LoadBuilding(name, cost, persec) { 
 
    var cur = buildings.length; 
 

 
    buildings[cur] = new Building(); 
 
    buildings[cur].Name = name; 
 
    buildings[cur].Cost = cost; 
 
    buildings[cur].PerSec = persec; 
 
} 
 

 
//The function used to gather money 
 
function GatherMoney() { 
 
    game.money++; //++ tells javascript to add 1 to the variable 
 

 
    //Display the player's current money 
 
    document.getElementById("money").innerHTML = game.money; 
 
} 
 

 
//The function that gets run every second 
 
function Tick() { 
 
    for (var i = 0; i < buildings.length; i++) { 
 
     game.money += game.buildings[i] * buildings[i].PerSec; 
 
    } 
 
    document.getElementById("money").innerHTML = game.money; 
 
} 
 

 
//The function to buy a lemonade stand 
 
function Build(id) { 
 
    if (game.money >= buildings[id].Cost) { //Check if the player has enough money, then subtract it and add a new building if they do 
 
     game.money -= buildings[id].Cost; 
 
     game.buildings[id] = game.buildings[id] + 1; 
 
     document.getElementById("money").innerHTML = game.money; 
 
     document.getElementById("Building1Qty").innerHTML = game.buildings[id]; 
 
    } 
 
} 
 

 
//Run this code once the page has loaded fully 
 
window.onload = function() { 
 
    InitBuildings(); 
 
    window.game = new GameSave(); 
 
};
<!--Pleae refer to Lesson 9.txt for a full description on this lesson --> 
 

 
<html> 
 
<head> 
 
<title>Basic Incremental Game</title> 
 
<link rel="stylesheet" type="text/css" href="css/Incremental.css"> 
 
<script src="js/Incremental.js"></script> 
 
</head> 
 

 
<body> 
 
\t <div id="page"> 
 
\t \t <div id="header"> 
 
\t \t \t <div id="game-title"> 
 
\t \t \t \t Basic Incremental Game 
 
\t \t \t </div> 
 
\t \t </div> 
 
\t 
 
\t <div id="content"> 
 
\t \t <div id="stats" class="block"> 
 
\t \t \t <div class="label">Money:</div> 
 
\t \t \t <div id="money" class="label">0</div> 
 
\t \t </div> 
 
\t 
 
\t \t <div id="clickables" class="block"> 
 
\t \t \t <input type="button" value="Click Me!" onclick="GatherMoney();"> 
 
\t \t </div> 
 
\t 
 
\t \t <div id="buildings" class="block"> 
 
\t \t \t <div id="Building1"> 
 
\t \t \t \t <input type="button" value="Lemonade Stand" onclick="Build(0);"> 
 
\t \t \t \t <div> 
 
\t \t \t \t \t <div class="label">Cost:</div> 
 
\t \t \t \t \t <div id="Building1Cost" class="label">10</div> 
 
\t \t \t \t </div> 
 
\t \t \t \t 
 
\t \t \t \t <div> 
 
\t \t \t \t \t <div class="label">Per Sec:</div> 
 
\t \t \t \t \t <div id="Building1PerSec" class="label">1</div> 
 
\t \t \t \t </div> 
 
\t \t \t \t 
 
\t \t \t \t <div> 
 
\t \t \t \t \t <div class="label">Quantity:</div> 
 
\t \t \t \t \t <div id="Building1Qty" class="label">0</div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t \t \t \t <div id="Building2"> 
 
\t \t \t \t <input type="button" value="Taco Stand" onclick="Build(1);"> 
 
\t \t \t \t <div> 
 
\t \t \t \t \t <div class="label">Cost:</div> 
 
\t \t \t \t \t <div id="Building2Cost" class="label">10</div> 
 
\t \t \t \t </div> 
 
\t \t \t \t 
 
\t \t \t \t <div> 
 
\t \t \t \t \t <div class="label">Per Sec:</div> 
 
\t \t \t \t \t <div id="Building2PerSec" class="label">1</div> 
 
\t \t \t \t </div> 
 
\t \t \t \t 
 
\t \t \t \t <div> 
 
\t \t \t \t \t <div class="label">Quantity:</div> 
 
\t \t \t \t \t <div id="Building2Qty" class="label">0</div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t </div> 
 

 

 
\t \t <div id="upgrades" class="block"> 
 
\t \t \t This is where our upgrades will go! 
 
\t \t </div> 
 
\t </div> 
 

 
</body>

EDIT:

Ich versuchte, die aber tit nur knapp sein Ziel zu ändern Arbeit

buildings[] 

zu

buildings["Lemonade Stand", "Taco Stand"] 
+0

Code in Frage stellen. Wenn Sie Links bereitstellen möchten, machen Sie diese bitte anklickbar. –

+0

ich habe sie klickbar gemacht es tut mir leid – s0l4rs7o4m

+0

Für eine Sache, in deiner GameSave Funktion, ändere 'this.buildings [i] = 0;' zu 'this.buildings [i] = Gebäude [i];' – pacifier21

Antwort

0

Wie wäre es,

LoadBuilding("myBuilding", 12, 1); 

Weil Sie diese Fabrik Funktion haben,

function LoadBuilding(name, cost, persec) { 
    var cur = buildings.length; 

    buildings[cur] = new Building(); 
    buildings[cur].Name = name; 
    buildings[cur].Cost = cost; 
    buildings[cur].PerSec = persec; 
} 
+0

funktioniert nicht, aber ich googelte, was eine Factory-Funktion ist. Ich muss den Code es drastisch wiederholen. – s0l4rs7o4m

+0

Was bekommen Sie, wenn Sie console.log (Gebäude)? – Luillyfe

0

Sie könnten eine Reihe von Bauobjekten dann durch sie durchlaufen bauen mit einer while-Schleife.

Siehe JSFIDDLE oder angehängter Code.

Lassen Sie mich wissen, wenn das hilft.

class Building { 
 
constructor(name, cost, persec) { 
 
    \t this.name = name; 
 
    this.cost = cost; 
 
    this.persec = persec; 
 
} 
 
} 
 

 
var buildings = []; 
 

 
buildings.push(new Building('Building One', '$10', '1')); 
 
buildings.push(new Building('Building Two', '$20', '0.5')); 
 
buildings.push(new Building('Building Three', '$25', '2')); 
 

 
var count = 0; 
 

 
while(count < buildings.length) { 
 
\t document.getElementById('stores').innerHTML += '<p>' + buildings[count].name + '<br />' + buildings[count].cost + '<br />' + buildings[count].persec + '</p>'; 
 
    count++; 
 
}
<div id="stores"> 
 

 
</div>

Verwandte Themen