Javascript 对于循环代码,每个循环x轴上的间距加倍

Javascript 对于循环代码,每个循环x轴上的间距加倍,javascript,jsx,photoshop-script,Javascript,Jsx,Photoshop Script,我试图在Photoshop中创建一个模式脚本,在整个画布上水平和垂直复制一个图像。但问题是,在x轴上,每个循环的值都会加倍。如果我移除j循环,它工作正常 这张照片将向你展示我所指的问题 该问题与如何使用偏移量有关: “平移”是指层的边界矩形。 如果将50px宽度的图像转换为50px,则生成的层宽度将为100px。 尝试在每次迭代中仅使用偏移量。如前所述,层[i,j]不是访问层的有效方法。我甚至不知道为什么会这样。你应该选择你的原始图层,复制并翻译它。大概是这样的: var width = act

我试图在Photoshop中创建一个模式脚本,在整个画布上水平和垂直复制一个图像。但问题是,在x轴上,每个循环的值都会加倍。如果我移除j循环,它工作正常

这张照片将向你展示我所指的问题


该问题与如何使用偏移量有关: “平移”是指层的边界矩形。 如果将50px宽度的图像转换为50px,则生成的层宽度将为100px。 尝试在每次迭代中仅使用偏移量。

如前所述,层[i,j]不是访问层的有效方法。我甚至不知道为什么会这样。你应该选择你的原始图层,复制并翻译它。大概是这样的:

var width = activeDocument.width.as("px");
var height = activeDocument.height.as("px");
var layer = app.activeDocument.activeLayer;
var layerWidth = layer.bounds[2] - layer.bounds[0];
var layerHeight = layer.bounds[3] - layer.bounds[1];
var copy, i, j;

var offset = parseInt(prompt("Type in the offset (spacing between pics) value here.\nDefault is 0px.", "0"));   

for (i = 0; i < width / (layerWidth + offset); i++)
{
    for (j = 0; j < height / (layerHeight + offset); j++)
    {
        // in the each loop we select the original layer, make a copy and offset it to calculated values
        app.activeDocument.activeLayer = layer;
        copy = layer.duplicate();
        copy.translate(i * (layerWidth + offset), j * (layerHeight + offset));
    }
}

layer.remove(); // remove the original layer
结果:


层[i,j]不是访问javascript数组第i行中第j项的有效方法。您需要层[i][j]。当我将其更改为该值并运行代码时,出现了这样一个问题:这意味着层不是数组的数组。我不熟悉photoshop脚本框架,但通过快速查看app.activeDocument.layers是一个层数组,无法进一步索引。如果要访问该层的层,需要根据和执行app.activeDocument.layers[i].layers[j]。这也不起作用。我猜photoshop脚本框架和illustrator有点不同。如果For循环不起作用,是否有其他方法可以实现这一点?即使删除offset变量,间距问题仍然会发生。我的循环码不是罪魁祸首?非常感谢!这很好:我还在习惯photoshop脚本框架祝你好运,它有点。。。特殊的
var width = activeDocument.width.as("px");
var height = activeDocument.height.as("px");
var layer = app.activeDocument.activeLayer;
var layerWidth = layer.bounds[2] - layer.bounds[0];
var layerHeight = layer.bounds[3] - layer.bounds[1];
var copy, i, j;

var offset = parseInt(prompt("Type in the offset (spacing between pics) value here.\nDefault is 0px.", "0"));   

for (i = 0; i < width / (layerWidth + offset); i++)
{
    for (j = 0; j < height / (layerHeight + offset); j++)
    {
        // in the each loop we select the original layer, make a copy and offset it to calculated values
        app.activeDocument.activeLayer = layer;
        copy = layer.duplicate();
        copy.translate(i * (layerWidth + offset), j * (layerHeight + offset));
    }
}

layer.remove(); // remove the original layer