簡體   English   中英

畫布中多次碰撞

[英]Multiple collision in canvas

我試圖使我的游戲暢通無阻。 我有很多對象,邊界發生沖突。 但是現在我陷入了物體之間的碰撞。 我對數組中的對象進行了循環,但是在最后一個對象上停止了。 每次與所選對象一起移動時,如何對每個對象進行碰撞檢查? 完整代碼在這里: 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

            }
        }

    }

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

因此,這是您的碰撞檢測功能。 請注意,我設置state = false循環之前一次,如果有沖突我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(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語句中也使用break

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM