AS:if语句评估错误

时间:2012-03-28 10:29:54

标签: actionscript-3 flash actionscript actionscript-2

我有一个非常简单的逻辑如下:

var divert:Number = 0;
for (var connection in _connections) {
    trace("target: " + _connections[connection].target + " || i: " + (i + 1));
    if(int(_connections[connection].target) != (i + 1)) {
        trace("bad connection");
        divert++;
    }
}

问题是当i + 1int(_connections[connection].target)相等时,if语句返回true,如下面trace()语句的输出中所示:

target: 0 || i: 1
bad connection
target: 1 || i: 1
bad connection

任何人都可以看到导致这种情况发生的原因吗?

编辑:根据请求包含的功能:

public function loadListener(i:Number, onProgress:Function, onComplete:Function):Void
    {
        trace("load listening to: "+i);
        trace("next in queue: " + _queues["lower"][0] + " | " + _queues["upper"][0]);
        _functions[i] = {onProgress:onProgress, onComplete:onComplete};
        if (_queues["lower"][0] != i + 1 || _queues["upper"][0] != i + 1) {
            var divert:Number = 0;
            for (var connection in _connections) {
                trace("target: "+_connections[connection].target+" || i: "+(i+1));
                if(int(_connections[connection].target) != (i + 1)) {
                    trace("bad connection");
                    divert++;
                }
            }
            if (divert == _connections.length) {
                _diversion = i + 1;
                trace("divert: "+divert+" || connections: "+_connections.length);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

首先,为什么在使用

时使用for(var)循环
var divert:Number = 0;
for each(var connection in _connections) {
    trace("target: " + connection.target + " || i: " + (i + 1));
    if(int(connection.target) != (i + 1)) {
        trace("bad connection");
        divert++;
    }
}

要进一步调试,请替换

trace("target: " + connection.target + " || i: " + (i + 1));

trace("target: " + int(connection.target) + " || i: " + (i + 1));

如果此描述为零,则表示问题出在哪里。

您可以尝试

if(connection.target.toString() != (i + 1).toString()) {