2012-02-07 122 views
4

我正在使用HTML5的canvas API製作遊戲,並且遇到了障礙。Javascript遊戲麻煩

其中一種遊戲效果是沿y軸向上傳播的導彈,受到風向(由渦輪發出)的陣風影響,導致其沿着x軸移動。製造其中一臺渦輪機我沒有問題,但是當它們的數量增加到3時,我遇到了麻煩。 在一個新的水平,我舉例說明這些渦輪機作爲對象,然後將其推到一個數組,因爲在這裏可以看到:

function gameStateNewLevel(){ 

    for (var i = 0; i < 2; i++){ 
     turbine = {}; 
     turbine.width = 10; 
     turbine.height = Math.floor(Math.random()*200); //turbine height 
     turbine.y = Math.floor(Math.random()*600) //turbine y-axis 
     if (Math.random()*10 > 5){ //indicates turbine side of canvas 
      turbine.side = leftSide; 
     }else{ 
      turbine.side = rightSide; 
     } 
     if(turbine.height <= 100){ //Increases turb. size if it's too small 
      turbine.height += Math.floor(Math.random()*100); 
     } 

     turbines.push(turbine); 

    } 

    context.fillStyle = "#FFFFFF"  
    switchGameState(GAME_STATE_PLAYER_START); 
} 

現在,他們正在渲染之前,他們還通過updateTurbine功能更新。所有這些功能應該做的是確保渦輪機不會相互重疊,並根據需要將它們向上或向下移動(通過循環訪問陣列並比較其中的每個對象)。我已經完成了這個功能,但是我在所有的循環之間完全失敗了。這是關於據我已經得到了,我有一種感覺,我在錯誤的軌道上:

function updateTurbines(){ 
    tempTurbine = {} 
    turbinePositionTop = tempTurbine.y; 
    turbinePositionBottom = tempTurbine.y + tempTurbine.height; 
    for (var i = turbineArrayLength; i < 2; i++){ 
     tempTurbine = turbines[i]; 
     for (var i = turbineArrayLength; i < 2; i++){ 
      if (tempTurbine !== tempTurbines[i]){ 
       while(){ 

       } 
      } 
     } 
    } 
} 

你可以找到源代碼,我沒有這個東西打破在www.techgoldmine了最遠。 com

+1

建議:你的'如果(turbine.height <= 100){'應該是一個'而(turbine.height <= 100){'在情況下,新數仍在100 – 2012-02-07 19:58:13

+0

把它在jsfiddle.net – 2012-02-07 19:59:28

+2

@Jeffrey或從一開始就使用'100 + Math.random()* 100' ... – Andrew 2012-02-07 20:05:13

回答

5

有代碼中的錯誤,在這裏看到的評論:

function updateTurbines(){ 
    tempTurbine = {} 
    turbinePositionTop = tempTurbine.y; // tempTurbine is an empty object right now so .y is undefined 
    turbinePositionBottom = tempTurbine.y + tempTurbine.height; // same problem here 
    for (var i = turbineArrayLength; i < 2; i++){ // why am I starting at the end of the array? What's the 2 for? 
     tempTurbine = turbines[i]; 
     for (var i = turbineArrayLength; i < 2; i++){ // why am I doing this twice? I'm also clobbering the value of "i" 
      if (tempTurbine !== tempTurbines[i]){ 
       while(){ 

       } 
      } 
     } 
    } 
} 

以下是我已經重寫,如果不知道什麼它做的:

function updateTurbines(){ 
    var l = turbines[i].length; // get the turbine length directly from the array[1] 
    for (var i = 0; i < l; i++){ // go through all of the turbines 
     var tempTurbine = turbines[i]; 
     turbinePositionTop = tempTurbine.y; // now .y is defined because tempTurbine is not an empty object 
     turbinePositionBottom = tempTurbine.y + tempTurbine.height; 
     for (var j = 0; j < l; j++) { // NOT i but j here 
      if (tempTurbine !== tempTurbines[j]){ 
       while(){ 
        // ... 
       } 
      } 
     } 
    } 
} 

[1]這可能會導致錯誤如果你修改數組。我假設你現在只添加到它。

+0

感謝您的回覆。明天我會給出這個鏡頭,告訴你結果如何,我真的很感激。 – styke 2012-02-07 20:55:48

+0

嘿,只是想說你很近,但它是錯的。 var l = turbines [i] .length; 這隻獲得當前對象的長度。 tempTurbine也沒有正確定義,在第8行應該是tempTurbine [j]。 無論哪種方式它沒有工作。我已經在答案中發佈了工作代碼。 – styke 2012-02-09 09:00:26

2

您的問題很可能是索引重疊;內循環正在改變外循環的i值。

我在內循環中將i更改爲j

function updateTurbines(){ 
    tempTurbine = {} 
    turbinePositionTop = tempTurbine.y; 
    turbinePositionBottom = tempTurbine.y + tempTurbine.height; 
    for (var i = turbineArrayLength; i < 2; i++){ 
     tempTurbine = turbines[i]; 
     for (var j = turbineArrayLength; j < 2; j++){ 
      if (tempTurbine !== tempTurbines[j]){ 
       while(){ 
        //What the heck is going on here? 
       } 
      } 
     } 
    } 
} 

讓我知道這是否修復它。

+0

也謝謝你,它不是一個固定的問題,只是澄清功能如何需要看。我也會記住你的建議。乾杯! – styke 2012-02-07 20:56:27

+0

考慮這個d – styke 2012-02-09 09:04:05

0

答覆很有幫助,但沒有一個能夠奏效......這是我最終得到的代碼。

function updateTurbines(){ 
    var l = turbines.length; 
    for (var i = 0; i < l; i++){ // go through all of the turbines 
     var tempTurbine1 = turbines[i]; 
     tempTurbine1.PositionTop = tempTurbine1.y; // now .y is defined because tempTurbine is not an empty object 
     tempTurbine1.PositionBottom = tempTurbine1.y + tempTurbine1.height; 
     for (var j = 0; j < l; j++) { // NOT i but j here 
      var tempTurbine2 = turbines[j]; 
      if (tempTurbine1 !== tempTurbine2 && tempTurbine1.PositionBottom >= tempTurbine2.PositionTop){ 

    } 
    } 
    } 
}