gpt4 book ai didi

javascript - Canvas 中的多次碰撞

转载 作者:行者123 更新时间:2023-12-03 07:55:57 25 4
gpt4 key购买 nike

我正在尝试制作解锁我的游戏。我有一系列对象,并且发生了边界碰撞。但现在我陷入了物体之间的碰撞。我对数组中的对象进行了循环,但它停止在最后一个对象上。每次与所选对象一起移动时,如何对每个对象进行碰撞检查?完整代码在这里: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,如果发生冲突,我将退出循环,因为没有必要检测其他冲突,因为标志是 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
}

Break 也用于 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"

关于javascript - Canvas 中的多次碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34800442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com