我正在使用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;
}}
}
谢谢。
[在编程中没有同时这样的东西(嗯,除非您是具有完美同步的多线程,这根本不是AS3的故事。]]
有很多方法可以接近该[[同时
事物: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方法::一次所有孩子。正如我在my answer to one of your previous questions中已经解释的那样,您可以遍历某个容器的所有子级,并且可以将它们放入-猜中什么-
var AB:Array = A.concat(B); var BC:Array = B.concat(C); var ABC:Array = A.concat(B).concat(C);
Case№2Array供以后使用。另外,您可以在执行过滤操作的同时过滤对象,然后仅放置您实际想要的对象。
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.