获取特定添加子项的数组

发布于 2024-10-31 19:48:56 字数 581 浏览 6 评论 0原文

我使用以下数组将子项添加到舞台:

for(var i=0;i<6;i++) {
    var aCherry=new cCherry()
    aCherry.y=10
    aCherry.x=10+100*i
    stage.addChild(aCherry)
}

现在我想根据另一个数组修改每个樱桃。像这样的东西:

var cherryLetter:Array=[1,0,0,0,0,0]
    for(i=0;i<6;++) {
        if(cherryLetter[i]) stage.getChildByName("aCherry")[i].y+=90
    }

显然 stage.getChildByName("aCherry")[i] 是不正确的,但是来自 JavaScript,这对我来说最有意义,并且应该准确地描述我想要实现的目标给正在读这篇文章的你们。那么,我实际上该怎么做呢?这是在某个名称 类下将一组子项添加到舞台中(因此如果需要,cCherry 数组也可以工作),然后以类似于上面循环的方式使用它们。

I'm using the following array to add children to the stage:

for(var i=0;i<6;i++) {
    var aCherry=new cCherry()
    aCherry.y=10
    aCherry.x=10+100*i
    stage.addChild(aCherry)
}

Now I want to modify each cherry based on another array. Something like this:

var cherryLetter:Array=[1,0,0,0,0,0]
    for(i=0;i<6;++) {
        if(cherryLetter[i]) stage.getChildByName("aCherry")[i].y+=90
    }

Clearly stage.getChildByName("aCherry")[i] isn't correct, but coming from JavaScript this makes the most sense to me and should accurately portray what I'm trying to achieve for you guys reading this. So, how would I actually do this? This being getting an array of children added to the stage under a certain name or class (so an array of cCherry would work too, if necessary), then using them in a way similar to the above loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硪扪都還晓 2024-11-07 19:48:56

以下是我对代码外观的建议,基于使用 getChildByName() 查找 cCherry 类的实例的愿望。请注意,我已将示例中的类名更改为 Cherry(我建议这样做,因为类名大写是 AS3 约定)。另外,最好以分号结束语句。虽然它通常是可选的,但在某些情况下,省略分号可能会导致很难跟踪运行时错误,因此我建议养成使用它们的习惯。我还建议在所有变量声明中包含 type,例如 var aCherry:Cherry 所示。

var i:int;
for(i=0; i<6; ++i)
{
    var aCherry:Cherry=new Cherry(); // Note, it's my recommendation that you rename cCherry class to Cherry (convention)
    aCherry.y=10;
    aCherry.x=10+100*i;
    aCherry.name = "aCherry" + String(i); // String() cast for clarity only, not necessary
    stage.addChild(aCherry);
}

var cherryLetter:Array=[1,0,0,0,0,0];
for(i=0; i<6; ++i)
{
    var cherry:Cherry = stage.getChildByName("aCherry" + String(i)) as Cherry;
    if(cherry && cherryLetter[i]) cherry.y += 90;
}

Here is my recommendation for how the code might look, based on the desire to use getChildByName() to find the instances of your cCherry class. Please note that I've changed the class name to Cherry in the example (which I recommend, since capitalizing class names is AS3 convention). Also, it's good practice to end statements with semi-colons. While it's usually optional, there are cases where omitting the semi-colon can produce very difficult to track down runtime bugs, so I recommend getting int he habit of using them. I also recommend including type in all your variable declarations, as shown with var aCherry:Cherry, for example.

var i:int;
for(i=0; i<6; ++i)
{
    var aCherry:Cherry=new Cherry(); // Note, it's my recommendation that you rename cCherry class to Cherry (convention)
    aCherry.y=10;
    aCherry.x=10+100*i;
    aCherry.name = "aCherry" + String(i); // String() cast for clarity only, not necessary
    stage.addChild(aCherry);
}

and

var cherryLetter:Array=[1,0,0,0,0,0];
for(i=0; i<6; ++i)
{
    var cherry:Cherry = stage.getChildByName("aCherry" + String(i)) as Cherry;
    if(cherry && cherryLetter[i]) cherry.y += 90;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文