同时操作多个数组

问题描述 投票:0回答:1

我正在使用ad3中的as3制作动画,以创建视频游戏。我用影片剪辑创建了多个数组,例如


var A:Array [mc1, mc2, mc3]
var B:Array [mc4, mc5, mc6]
var C:Array [mc7, mc8, mc9]
var anObject:DisplayObject;

如何同时操作所有阵列中的所有影片剪辑?我已经试过了:


mc_Player.addEventListener(MouseEvent.CLICK, Change_Position);
function Change_Position (event:MouseEvent):void
{
    {for each (anObject in A) & (anObject in B) & (anObject in C) // how can I get this right?
    {anObject.x -= 100;
    }}
}

我还试图了解是否可以在不使用数组的情况下同时操作项目中的所有影片剪辑。

例如

mc_Player.addEventListener(MouseEvent.CLICK, Change_Position);
function Change_Position (event:MouseEvent):void
{
    {all movie_clips // how can I get this right?
    {anObject.x -= 100;
    }}
}

谢谢。

arrays actionscript-3 adobe
1个回答
0
投票

[在编程中没有同时这样的东西(嗯,除非您是具有完美同步的多线程,这根本不是AS3的故事。]]

有很多方法可以接近该[[同时

事物:
    将所有对象放入单个容器中。您将能够更改该容器的
  1. x和y,因此所有嵌入对象将立即更改其可见位置。缺点是您无法单独旋转或缩放它们(想像它们被夹在板上并想象您旋转了整个板),否则将无法移动它们的一半。
  • Array
  • 和循环。您可以非常快速地一个一个地遍历所有项目。从外面看,它[,但从不。
    话虽如此,为了实现您想要的事情,您需要找到一种方法将所有要处理的对象[同时]放入单个Array中,然后执行您想做的事情他们。

    Case№1:很多Array

    到一个。

    // This methods takes a mixed list of items and Arrays // and returns a single flattened list of items. function flatten(...args:Array):Array { var i:int = 0; // Iterate over the arguments from the first and on. while (i < args.length) { if (args[i] is Array) { // These commands cut the reference to the Array // and put the whole list of its elements instead. aRay = [i,1].concat(args[i]); args.splice.apply(args, aRay); } else { // The element is not an Array so let's just leave it be. i++; } } return args; } 然后您需要做的就是从几个Array

    中得到一个列表:
    var ABC:Array = flatten(A, B, C);
    
    for each (var anObject:DisplayObject in ABC)
    {
        anObject.x -= 100;
    }
    

    性能方面,如果逻辑上可能的话,预组织这些Array

    是个好主意,因此您不必在每次需要处理所有对象时都进行编译。简而言之,如果有时您需要A + B,有时需要B + C,有时又需要A + B + C,则只需创建它们并准备好。如果您知道要处理的内容,则甚至不需要复杂的
    flatten
    方法:

    var AB:Array = A.concat(B); var BC:Array = B.concat(C); var ABC:Array = A.concat(B).concat(C); Case№2

    :一次所有孩子。正如我在my answer to one of your previous questions中已经解释的那样,您可以遍历某个容器的所有子级,并且可以将它们放入-猜中什么-
    Array
    供以后使用。另外,您可以在执行过滤操作的同时过滤对象,然后仅放置您实际想要的对象。

    var ALL:Array = new Array; // Iterate over the amount of children of "this" container. for (var i:int = 0; i < numChildren; i++) { // Obtain a reference to the child at the depth "i". var anObject:DisplayObject = getChildAt(i); // Here go possible criteria for enlisting. // This will ignore texts and buttons. // if (anObject is MovieClip) // This will take only those with names starting with "mc". if (anObject.name.substr(0, 2) == "mc") { ALL.push(anObject); } } // Viola! You have all the objects you want in a single Array // so you can bulk-process them now, which is ALMOST simultaneous.

    © www.soinside.com 2019 - 2024. All rights reserved.