2017-05-29 54 views
-1

我不確定如何按陣列添加新建築物。我是一個初學者的JavaScript人。添加新建築物的javascript

我在其他方面加入了保存/加載到後端,但客戶端由於某種原因給我提供了問題。

我認爲這與我不是站在陣列正確的位置有關,但如果你能指向正確的方向,我很樂意學習。

我想添加第二個建築稱爲

loadbuilding("taco stand") 

下面是代碼:

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>

編輯:

我試圖改變,但針鋒相對didnt工作

buildings[] 

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

把你的代碼中的問題。如果您要提供鏈接,請讓它們點擊。 – 2017-05-29 03:53:08

+0

我讓他們點擊對不起 – s0l4rs7o4m

+0

首先,在你的GameSave函數中,將'this.buildings [i] = 0;'改爲'this.buildings [i] = buildings [i];' – pacifier21

回答

0

怎麼樣,

LoadBuilding("myBuilding", 12, 1); 

因爲你有這樣的工廠函數,

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

不起作用,但我googled什麼是工廠功能。我需要重做代碼。 – s0l4rs7o4m

+0

當你console.log(建築物)時,你會得到什麼? – Luillyfe

0

你可以建造建築物對象的數組,然後遍歷它們使用一個while循環。

請參閱JSFIDDLE或附加的代碼。

讓我知道是否有幫助。

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>