画布中的多次碰撞

时间:2016-01-14 21:50:33

标签: javascript canvas collision

我正试图解开我的游戏。我有一些对象,我使边界碰撞。但现在我陷入了物体之间的碰撞。我在数组中对象进行了循环,但是在最后一个对象上停止了。每次使用所选对象移动时,如何对每个对象进行碰撞检查? 这里有完整的代码: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 个答案:

答案 0 :(得分:1)

检查其他对象时,您正在重置valid标志。

所以这是你的碰撞检测功能。请注意,我在循环之前设置state = false一次,如果在循环中发生冲突I break,因为没有检测到其他冲突的点,因为标志是true或{{1 }}。如果您检测到碰撞,则将标志设置回false除最后一个对象外的所有对象。

false

<强>歇。

Break是一个javascript保留令牌,用于打破循环和切换。

For loop

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.
        }
    }

}

while循环

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

while(i < 10){ if(i === 2){ break; // exit out of the while loop } } 循环也是如此。

Break只退出当前循环;

do{ }While()

Break也用于switch语句

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
}

要防止这种情况使用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