我是JavaScript的新手,一直在尝试给Leap Motion的输出数据添加一个平滑过滤器。我使用Cylon.js获得数据,它基本上输出3个值(x,y和z)。然而,我不能让平滑代码工作,我想这是因为我习惯了C/C++语法,可能做错了什么。
代码是这样的:
"use strict";
var Cylon = require("cylon");
var numReadings = 20;
var readings[numReadings];
var readIndex = 0;
var total = 0;
var average = 0;
for (var thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
Cylon.robot({
connections: {
leapmotion: {
adaptor: "leapmotion"
}
},
devices: {
leapmotion: {
driver: "leapmotion"
}
},
work: function(my) {
my.leapmotion.on("hand", function(hand) {
console.log(hand.palmPosition.join(","));
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = hand.palmPosition;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
console.log(average);
});
}
}).start();因此,我试图过滤的数据是"hand.palmPosition“。但它在控制台上给出了以下错误:

如有任何帮助,我们不胜感激!
谢谢
发布于 2016-04-28 05:19:06
无效的JS:
var readings[numReadings];看起来你想让readings成为一个数组。您不需要初始化JS数组的大小。要创建数组,请执行以下操作:
var readings = [];要用零填充它:
for (var thisReading = 0; thisReading < numReadings; thisReading++) {
readings.push[0];
}https://stackoverflow.com/questions/36900872
复制相似问题