2013-11-04 85 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