简体   繁体   English

海盗–写回一个JavaScript数组

[英]Pirate – write back a javascript array

having problems with a little javascript. 有一些JavaScript问题。 Pixate is UI design tool that allows you to build custom actions in JS. Pixate是UI设计工具,可让您在JS中构建自定义动作。 I am trying to build a little JS action with a few lines of code, but obviously i am overseeing something evident (sorry, i am not a real coder). 我正在尝试用几行代码构建一个小的JS操作,但是显然我正在监督一些显而易见的事情(对不起,我不是真正的程序员)。 Here is my script so far: 到目前为止,这是我的脚本:

var layers = getSelectedLayers();
var erg = "";

for (var elem in layers) {
  erg += layers[elem] + ", ";
}

alert(erg);

var group = createLayer("MyGroup");
group.y = group.x = 0;
group.width = Screen.width;;
group.height = Screen.height;;
group.backgroundColor = 'transparent';

nestLayers(group, erg);

The commands "getSelectedLayers" "createLayers" ans "nestLayers" are offered by Pixate. Pixate提供了命令“ getSelectedLayers”,“ createLayers”和“ nestLayers”。 If i try to run my code, the "getSelectedLayers" seems to be working (the alert function gives back an array of the selected layers). 如果我尝试运行代码,则“ getSelectedLayers”似乎正在运行(警报功能会返回一组选定图层)。 Creating the new layer works also. 创建新层也可以。 The problem is, that after starting the "nestLayers" function, after nesting the first selected layer the script stops with an error on my last line "undefined is not a function"... 问题是,在启动“ nestLayers”函数之后,嵌套第一个选定的图层后,脚本会在我的最后一行“未定义不是函数”的错误处停止运行...

Any help is greatly appreciated – Thanks! 非常感谢您的帮助-谢谢!

There are two issues with your nestLayers call: it expects all the layers as parameters (not an array), and you were passing an array of strings, not layer objects (see the docs here: http://www.pixate.com/docs/actions/#nestlayer ). nestLayers调用存在两个问题:它希望将所有图层作为参数(而不是数组),并且您正在传递字符串数组,而不是图层对象(请参见此处的文档: http : //www.pixate.com/ docs / actions /#nestlayer )。

The correct action should look like this: 正确的操作应如下所示:

var layers = getSelectedLayers();

var group = createLayer("MyGroup");
group.y = group.x = 0;
group.width = Screen.width;
group.height = Screen.height;
group.backgroundColor = 'transparent';

nestLayers.apply(this, [].concat(group, layers));

apply calls a function with the supplied array as parameters ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply ), and I'm just concatenating the group layer and the rest of the layers into a single array. apply用提供的数组作为参数调用一个函数( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply ),而我只是串联组层并将其余各层合并为一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM