如何插入变量dat.Gui一个数组? [英] How to insert variables to dat.Gui with an array?

查看:882
本文介绍了如何插入变量dat.Gui一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着通过一个数组来声明变量在dat.Gui控制器来使用它们。这是我尝试:

I try to declare Variables via an array to use them in an dat.Gui controller. This is my attempt:

<script>
  window.Horizontale = 300;
  window.Vertikale = 300;
  window.A = new Array();

  for (var i=1; i<7; i++) {
    window.A[i] = false;
  }

  window.onload = function() {
    var gui = new dat.GUI();
    gui.add(window, 'Horizontale', 0, 600);
    gui.add(window, 'Vertikale', 0, 600);

    for (var i=1; i<7; i++) {
      gui.add(window, A[i]);
    }
    gui.remember(window);
  };
</script>

数组中的变量不显示在GUI中。我真的很新的JavaScript,所以也许这仅仅是声明的问题?

The variables in the array dont appear in the GUI. I am really new to Javascript, so maybe it is just a problem of the declaration?

推荐答案

在这一行:

gui.add(window, A[i]);

... 添加()看起来对象窗口与在名称的属性 A [I] ,在这种情况下,计算结果为

add() looks on the object window for a property with the name specified by A[i], which in this case evaluates to false.

然而,有在窗口没有名为虚假的财产。所以,你需要用你想要的名称某处指定对象的属性,然后你就可以将它们赋值你想传递的附加参数添加() - 在这种情况下,会给你一个复选框未选中状态

However, there's no property named false on window. So you need to specify properties on an object somewhere with the names you want, and then you can assign each of them the values you want to pass as the additional argument to add() – In this case, false would give you a checkbox in the unchecked state.

所以假设你想6名为1选中复选框 - 6,有一些事情可以做,但它们都涉及添加6命名属性为对象的某个地方。下面是使用第二个对象来保存你的控制器名称,将取代你的第二个循环做这件事:

So assuming that you wanted 6 unchecked checkboxes named 1 - 6, there are a few things you could do, but they all involve adding 6 named properties to an object somewhere. Here's one way to do it using a second object to hold your controller names, which would replace your second for loop:

for (var i=1; i<7; i++) {
  controller_names[i] = A[i];
  gui.add(controller_names, i, A[i]);
}

然而,这可能不是很好地与保存设置功能的发挥 - dat.gui似乎只能保存设置的GUI对象本身定义的控制,如下探讨:<一href=\"http://stackoverflow.com/questions/24335504/save-dat-gui-$p$psets-for-dynamically-added-controls\">Save dat.gui preset时动态添加控件?

...在这种情况下,你可以尝试更多的东西是这样的:

…in which case, you might try something more like this:

<script>
  window.onload = function() {

    var gui = new dat.GUI();
    gui.Horizontale = 300;
    gui.Vertikale = 300;
    gui.A = new Array();

    for (var i=1; i<7; i++) {
      gui.A[i] = false;
    }

    gui.add(gui, 'Horizontale', 0, 600);
    gui.add(gui, 'Vertikale', 0, 600);

    for (var i=1; i<7; i++) {
      gui.add(gui.A, i, gui.A[i]);
    }
    gui.remember(gui);
  };
</script>

这篇关于如何插入变量dat.Gui一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆