同时操作多个阵列 [英] Operate multiple arrays simultaneously

查看:252
本文介绍了同时操作多个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using as3 in adobe animate to create a video game. I have created multiple arrays with movie clips e.g.


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

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

How can I operate all movie clips in all arrays simultaneously? I have tried this:


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;
    }}
}

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

I am also trying to understand if there is a way to operate all movie clips in my project simultaneously without using arrays.

例如

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的故事.

There's no such thing as simultaneous in programming (well, unless you are multi-threading with the perfect sync, which is not an AS3 story at all).

有很多方法可以接近同时:

  1. 将所有对象放入单个容器中.您将能够更改该容器的 x y ,以便所有嵌入的对象将立即更改其可见位置.不利的一面是,您无法单独旋转或缩放它们(想像它们被夹在板上,然后想象您旋转了整个板),否则您将无法移动它们的一半.
  2. 数组和循环.您可以非常快速地一个一个地遍历所有项目.从外面看起来是同时,但从来没有.
  1. Put all the objects into a single container. You will be able to change x and y of that container so all the in-laid objects will change their visible positions at once. The downside is that you cannot rotate or scale them individually (think of them as clipped to a board and imagine you rotate the whole board), or you won't be able to move half of them.
  2. Arrays and loops. You iterate through all the items one by one, very fast. It looks simultaneous from the outside, but it never is.

话虽如此,为了实现所需的目标,您需要找到一种方法,将要同时同时处理的所有对象放入单个数组,然后然后在他们身上做你想做的事.

All that said, in order to achieve the things you want you need to figure a way to put all the objects you want to process simultaneously into a single Array and then do the thing you want on them.

№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 中获得一个列表:

Then all you need to do is to get a single list out of your several Arrays:

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 方法:

Performance-wise, it is a good idea to pre-organize, if logically possible, these Arrays so you don't have to compile them each time you need to process all the objects. Simply, if sometimes you would need A + B, and sometimes B + C, and sometimes A + B + C, just create them and have them ready. If you know what you are going to deal with, you won't even need that complicated flatten method:

var AB:Array = A.concat(B);
var BC:Array = B.concat(C);

var ABC:Array = A.concat(B).concat(C);

№2:一次所有的孩子.正如我在我对您以前的问题之一的回答中所解释的那样,您可以遍历某个容器的所有子容器,然后您可以将它们放入-猜猜是什么- Array 供以后使用.另外,您可以在执行此操作时过滤对象,并仅将您实际想要的对象放在其中.

Case №2: all the children at once. As I already explained in my answer to one of your previous questions, you can iterate over all the children of a certain container, and you can put them into — guess what — Array for later use. Also, you can filter the objects while doing so and put only those ones you actually want to be there.

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. 

这篇关于同时操作多个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆