2013-11-04 129 views
0

任何人都可以幫助我在數組部分?我正在製作一款彩色拖放遊戲,將項目拖入一個框中,並在錯誤或正確時通知他人。需要數組幫助(調用數組)

我如何調用該陣列由:

var items=[] 
items.push({x:400,y:330,width:30,height:30,color:"rgba(100,223,0, 0.5)",isDragging:false}); 
items.push({x:300,y:330,width:30,height:30,color:"rgba(220,0,0, 0.5)",isDragging:false}); 
items.push({x:200,y:330,width:30,height:30,color:"rgba(220,223,0, 0.5)",isDragging:false}); 

到:

if (items[0].push(x > 400 && x <175 && y>50 &&y<235)){ 
//console.log("ok"); 
canvas2.innerHTML = "Score:" + 1; 

} 

else if(x > 255 && x < 376 && y > 50 && y <235){ 
    canvas2.innerHTML = "WRONG! please try again." 
    rect = false; 
    myMove = false; 
} 
    else if(x > 455 && x < 576 && y > 50 && y <235){ 
    canvas2.innerHTML = "WRONG! please try again." 
    rect = false; 
    myMove = false; 

} 
+0

什麼是'如果(項目[0] .push(X> 400 && X <175 && y> 50 &&ÿ< 235))「應該在做什麼? –

+0

你想在這裏做什麼?你能多解釋一下嗎? –

+0

獲取鼠標的位置 – Untitled

回答

0

這將只推一個布爾值數組:

if (items[0].push(x > 400 && x <175 && y>50 &&y<235)){ ... 

這是第一次評估:

x > 400 && x <175 && y>50 && y < 235 

然後,該結果(真或假)被推送到您的數組(並沒有別的)。您的條件檢查基本上檢查push返回的長度總是非零,所以您的條件是真實的,並將始終使用。

你的條件也是「不可能」爲x不能高於400和低於175在同一時間...

要解決你需要改變了一下週圍的代碼這一特定問題。我不知道你打算如何使用它,因爲將結果推送到你的數組與你已經存儲在那裏的內容不匹配,如果你打算把x和y存儲爲新對象,你需要創建一個JSON字符串。

你可以代替試試這個:

if (x > 175 && x < 400 && y > 50 && y < 235) { 
    ... 
} else if ... 

(代替...有實際的代碼)

+0

感謝您的解釋。但是我可以知道如何獲得數據items.push({x:400,y:330,width:30,height:30,color:「rgba(100,223,0,0.5)」,isDragging:false});從var items = [] items.push({x:400,y:330,width:30,height:30,color:「rgba(100,223,0,0.5)」,isDragging:false}); items.push({x:300,y:330,width:30,height:30,color:「rgba(220,0,0,0.5)」,isDragging:false}); items.push({x:200,y:330,width:30,height:30,color:「rgba(220,223,0,0.5)」,isDragging:false});到if else語句? – Untitled

+0

@ user2953775使用索引:'items [0]'會給你第一個項目。 'var item = items [0]; var x = item.x; // etc'。你可能想用一個循環遍歷項目來檢查,而不是固定的索引。 – K3N

+0

非常感謝。它有很多幫助。 – Untitled