控制台日志中出现意外的碰撞功能序列

时间:2014-12-12 13:18:03

标签: javascript object mouseclick-event

我遇到了这个功能的问题。奇怪的控制台日志og'我'。

1个事件的

console.log 示例:

0
0
1
2
3
4
5
6
7
8
9 times 0 ?? <--- wtf
0
9 times 0 ?? <--- wtf

传入函数的'rect'对象的长度为9。 的功能

function collides(rect, x, y) {
    // check if a click/tap x, y coordiantes 'collide' with a rectangle in use.
    var isCollision = false, i, left = 0, right = 0, top = 0, bottom = 0, lengd = rect.length;
    for (i = 0; i < lengd; i += 1) {
        left = rect[i].x;
        right = rect[i].x + rect[i].w;
        top = rect[i].y;
        bottom = rect[i].y + rect[i].h;
        console.log(i); <------------ THIS ONE HERE--------------
        if (right >= x && left <= x && bottom >= y && top <= y) {
            isCollision = rect[i];
            break;
        }
    }
    return isCollision;
}

来源:9 * 0的这里我基本上只是尝试识别正在使用的矩形。

                for (var i = 0; i < lengd; i += 1) {
                    // Identify the rectangle in use.
                    console.log("for loop"); <--- gets called 9 times
                    if (collides(rects[i], ex, ey)) { <--- collides logs 9 times 0's
                        var rightBox = rects[i];
                        var rectangle = rects2[i];
                    }
                }

0 个答案:

没有答案