【问题标题】:Uncaught TypeError: Cannot read property 'length' of undefined (array)未捕获的类型错误:无法读取未定义(数组)的属性“长度”
【发布时间】:2013-07-24 17:50:38
【问题描述】:

下面是我正在为我的 Phonegap 项目编写的一段代码。

JSFiddle:http://jsfiddle.net/wC79k/

我正在尝试使用提供的角度(sensorAngles 数组)绘制曲线。这个数组将通过蓝牙更新(首先作为字符串发送),因此我只需要在接收到的数据没有损坏的情况下更新曲线(也就是每个数组中没有空或假元素,全长)。

我现在正在通过 setTimeout 更新角度数组来“模拟”蓝牙串行更新。

目前,脚本按预期工作(当数组/字符串损坏时曲线不会绘制/更新,一旦收到非损坏数组就会继续更新),但我收到上述错误(未捕获的类型错误)。

这似乎与第 26 行有关,其中损坏的数组使 sensorAngles 未定义(因此 .length 不起作用)。

for (var i = 0; i < (sensorAngles.length); i++){
    sensorPos.push({
        x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
        y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
    });

}

我已尝试向其添加 if 语句,但曲线将不再更新。

我 2 天前刚开始使用 Javascript 和 Phonegap,所以任何帮助都会很棒!

干杯, 京东

完整代码:

var stage = new Kinetic.Stage({
    container: 'myCanvas',
    width: window.innerWidth,
    height: window.innerHeight
});

var sensorLayer = new Kinetic.Layer();

/* initialise an empty array of all the angles */
var sensorAngles = [0, 0, 0, 0, 0, 0, 0, 0];
//var sensorPos = [];

/* divide each sensor distance segment by height of screen/window for
maximum amount of canvas being used */
var sensorDistance = (stage.getHeight() / ((sensorAngles.length) + 2));

function diffPos(angle){ // angles in radians
    return {
        dx: sensorDistance * Math.sin(angle),
        dy: sensorDistance * Math.cos(angle)
    }
};

function calcSpline(sensorAngles){
    sensorPos = [{x: (stage.getWidth() / 2), y: (stage.getHeight() - sensorDistance)}];
    for (var i = 0; i < (sensorAngles.length); i++){
        sensorPos.push({
            x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
            y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
        });

    }
    return sensorPos;
};

calcSpline(sensorAngles);

var sensorPos = calcSpline(sensorAngles);
var spine = new Kinetic.Spline({
    points: sensorPos,
    stroke: 'red',
    strokeWidth: 2,
    lineCap: 'round',
    tension: 0.3
});

sensorLayer.add(spine);
stage.add(sensorLayer);



function updateSpline(angles){
    calcSpline(sanityCheck(angles));
    spine.setPoints(sensorPos);
    sensorLayer.draw();
};

function sanityCheck(data) {
    data = data.split(",");
    if (data.filter(function(n){return n}).length === sensorAngles.length) {
        sensorAngles = data;
        return sensorAngles;
    }
}

setTimeout(
    function() {
        updateSpline("0.1,0.12,0.08,-0.2,0.1,0.85,0.1,-0.2")
    },1000
);
setTimeout(
    function() {
        updateSpline("0.1,0.2,0.1,-0.1,0.1,0.1,0.1,-0.2")
    },2000
);
setTimeout(
    function() {
        updateSpline("0.1,0.12,0.08,-0.2,0.1,0.85,0.1,-0.2")
    },3000
);
setTimeout(
    function() {
        updateSpline("0.4,0.5,,-0.6,0.2,0.96,0.02,-0.01") //corrupt 
    },4000
);
setTimeout(
    function() {
        updateSpline("0.1,,0.08,-0.2,0.1,0.85,0.1,-0.2") // corrupt
    },5000
);

setTimeout(
    function() {
        updateSpline("0.6,0.12,0.22,-0.2,0.1,0.85,0.1,-0.2")
    },6000
);

【问题讨论】:

    标签: javascript cordova kineticjs


    【解决方案1】:

    如果没有得到长度,您可以尝试 console.log() 并检查您是否在控制台中获得写入值?

    示例

    console.log()
    for (var i = 0; i < (sensorAngles.length); i++){
        sensorPos.push({
            x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
            y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
        });
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 try...catch 块来忽略损坏的数组。

      http://jsfiddle.net/wC79k/1/

      try {
      for (var i = 0; i < (sensorAngles.length); i++){
          sensorPos.push({
              x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
              y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
          });
      
      }
      }catch(err){
          // console.log("oops");
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-24
        • 2021-05-03
        • 2019-11-22
        • 2020-07-19
        相关资源
        最近更新 更多