2016-01-14 40 views
0

我試圖解鎖我的遊戲。我有一些對象,我做了邊界碰撞。但是現在我被對象之間的碰撞困住了。我在數組中循環了對象,但它停在最後一個對象上。我每次移動選定對象時如何對每個對象進行碰撞檢查? Full code here:http://foxfcb.sweb.cz/我是編程新手,所以請耐心等待。畫布中的多次碰撞

canvas.addEventListener('mousemove', function (e) { 

    ... 

     var shapes = myState.shapes; 
     var l = shapes.length; 

     for (var i = 0; i < l; i++) { 

      var shape = myState.shapes[i]; 
      var selection = myState.selection; 

      // collision between objects 
      if (selection.x < (shape.x + shape.w) && (selection.x + selection.w) > shape.x && 
       selection.y < (shape.y + shape.h) && (selection.y + selection.h) > shape.y) { 
       myState.valid = true; //stop 
      } 
      // boundaries collision 
      else if (myState.selection.x < 0 || myState.selection.y < 0 || myState.selection.x + myState.selection.w > 600 || myState.selection.y + myState.selection.h > 600) { 
       myState.valid = true; //stop 
      } 

      else { 
       myState.valid = false; //moving 

      } 
     } 

    } 

回答

1

當您檢查其他對象時,您正在重置valid標誌。

所以這裏是你的碰撞檢測功能。注意我在循環之前設置了state = false一次,並且如果出現碰撞I break在循環之外,因爲沒有點檢測到其他衝突,因爲標誌是truefalse。如果檢測到碰撞,您將標誌設置回false而不是最後一個對象。

var textCollision = function(){ 
    var shapes, l, shape, selection, i; 
    shapes = myState.shapes; 
    l = shapes.length; 
    myState.valid = false; // assume no collision 
    for (i = 0; i < l; i++) { 
     shape = myState.shapes[i]; 
     selection = myState.selection; 
     if (selection.x < (shape.x + shape.w) && (selection.x + selection.w) > shape.x && 
      selection.y < (shape.y + shape.h) && (selection.y + selection.h) > shape.y) { 
      myState.valid = true; //stop 
      // there is no point testing any others as it will make no dif 
      break; // step out of the for loop. 
     } 
     // boundaries collision 
     else if (myState.selection.x < 0 || myState.selection.y < 0 || myState.selection.x + myState.selection.w > 600 || myState.selection.y + myState.selection.h > 600) { 
      myState.valid = true; //stop 
      // there is no point testing any others as it will make no dif 
      break; // step out of the for loop. 
     } 
    } 

} 

打破。

Break是一個JavaScript保留的令牌,用於打破循環和開關。

For循環

for(i = 0; i < 10; i++){ 
    if(i === 2){ 
     break; // exit the loop at 2 
    } 
} 

While循環

while(i < 10){ 
    if(i === 2){ 
     break; // exit out of the while loop 
    } 
} 

也做對do{ }While()循環一樣。

Break僅退出當前循環;

for(j = 0; j < 10; j++){ // j loop 
    for(i = 0; i < 10; i++){ // i loop 
     if(i === 2){ 
      break; // exit the i loop at 2 
     } 
    } 
    // the break moves execution to the first line after the loop it is in 
    // the j loop continues as normal 
} 

休息也被用在switch語句

function num(i){ 
    switch(i){ 
    case 1: 
     console.log("one"); 
     // no break token so execution continues inside the switch 
    case 2: 
     console.log("two"); 
    } 
} 
num(1); // outputs "one" "two" 

爲了防止這種使用break

function num(i){ 
    switch(i){ 
    case 1: 
     console.log("one"); 
     break; // break out of the switch 

    case 2: 
     console.log("two"); 
     // no point having a break here as it is at the end anyways 
    } 
    // break moves execution to here 
} 
num(1); // outputs "one" 
+0

對不起,我沒有寫信給你。這是休息的好主意,但還有一個錯誤。數組中包含的對象實際上是在移動。因此它在開始時通過條件並停止移動。我需要提取對象,我實際上是從數組中移出。但我認爲我可以做到。非常感謝。真正appriciate你的幫助。 –

+0

好的,我爲我的陣列做了條件,它工作正常! :) if(shapes [i]!== myState.selection){ ... } –