首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在node.js和cylon.js上平滑代码,但语法失败

尝试在node.js和cylon.js上平滑代码,但语法失败
EN

Stack Overflow用户
提问于 2016-04-28 05:05:02
回答 1查看 75关注 0票数 0

我是JavaScript的新手,一直在尝试给Leap Motion的输出数据添加一个平滑过滤器。我使用Cylon.js获得数据,它基本上输出3个值(x,y和z)。然而,我不能让平滑代码工作,我想这是因为我习惯了C/C++语法,可能做错了什么。

代码是这样的:

代码语言:javascript
复制
"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“。但它在控制台上给出了以下错误:

如有任何帮助,我们不胜感激!

谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-04-28 05:19:06

无效的JS:

代码语言:javascript
复制
var readings[numReadings];

看起来你想让readings成为一个数组。您不需要初始化JS数组的大小。要创建数组,请执行以下操作:

代码语言:javascript
复制
var readings = [];

要用零填充它:

代码语言:javascript
复制
for (var thisReading = 0; thisReading < numReadings; thisReading++) {
  readings.push[0];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36900872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档