gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'length' of undefined (array)

转载 作者:行者123 更新时间:2023-11-30 05:46:14 25 4
gpt4 key购买 nike

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

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

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

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

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

这似乎与第 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
);

最佳答案

如果没有得到长度,你可以尝试 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
});
}

关于javascript - 未捕获的类型错误 : Cannot read property 'length' of undefined (array),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17841287/

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