在舞台as3上移动多个符号 [英] moving multiple symbols across stage as3

查看:48
本文介绍了在舞台as3上移动多个符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的视频游戏,其中我需要以相同的速度在舞台上移动50多个符号.我想使用一个as3命令,通过它我可以同时定位所有符号.目前,我已将符号一一添加:

I am working on a simple video game in which I need to move 50+ symbols across the stage with the same speed. I would like to use a single as3 command with which I can target all symbols at the same time. Currently I have added the symbols one by one:

用于将符号向右移动的代码段:

Code snippets for moving symbols to the right:

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_3);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_3);

 function fl_MoveInDirectionOfKey_3(event:Event)
{
    if  (rightPressed) {
        mc1.x = 5;
        mc2.x = 5;
        mc3.x = 5;
        mc4.x = 5; 
        and so on....,
}

如何一次性将功能应用于所有mc符号?

What can I do to apply the function to all mc symbols in one go?

谢谢!

推荐答案

循环.当您需要以相同的方式寻址多个对象时,可以使用循环.循环是对给定数量的任何对象(实际上是2个,10个,100个或1000000个)执行相同动作过程的方式.

Loops. When you need to address multiple objects in the same manner, you use loops. Loop is the way to perform the same course of actions on any (literally any) given number of objects, be it 2, or 10, or 100, or 1000000.

首先,您需要一种方法来解决将要使用的所有对象.当您说"循环"时,您通常会牢记"数组"或"列表".循环和数组相处得很好.

First of all, you need a way to address all the objects you are going to work with. When you say 'loop' you mostly keep 'array' or 'list' in mind. Loops and arrays get along very well.

最直接的方法是创建一个包含所有项目的数组:

The most straightforward way is to create an Array with all the items:

var A:Array = [mc1, mc2, mc3, /* and so on */ , mc50];

但是,其中没有任何美感,而且如果您突然想将这些对象的数量从50增加到150,那么您将需要完成许多繁琐的工作.

However, there's no beauty in it, and also if you suddenly want to increase the number of these objects from 50 to 150, you will have a lot of tedious work to do.

幸运的是,您的剪辑易于命名,因此您可以通过构造其名称然后通过方法 getChildByName(...);

Luckily, your clips are named handily, so you can access by constructing their names and then through method getChildByName(...);

// Define an empty Array.
var A:Array = new Array;

// A temporary variable to hold a name of the clip.
var aName:String;

// A temporary variable to refer your multiple clips one by one.
var anObject:DisplayObject;

// This loop performs 50 times with i bearing the value of
// 1, 2, 3 and so forth to 50 each separate time.
for (var i:int = 1; i <= 50; i++)
{
    // Construct a clip name from letters "mc" and the
    // text representation of i, so it first constructs "mc1"
    // then "mc2" then "mc3" and so forth up to "mc50".
    aName = "mc" + i;

    // This operation finds an object with the given instance name.
    anObject = getChildByName(aName);

    // Put the found object into the Array.
    A.push(anObject);
}

现在,如果您希望从50增加到150,只需将 i< = 50 更改为 i< = 150 就可以了.

Now if you want to go up from 50 to 150 you just change i <= 50 to i <= 150 and that's it.

那么,如果这些剪辑确实以"mc"和一些数字命名,但是您不确定数字从1到50是否完整且没有任何间隔,该怎么办?说,没有"mc3"?上面的脚本将形成 Array ,其中包含 null 项:[mc1,mc2,null,mc4,mc5,...,mc50].不好.不过,还有另一条路要走.

So, what if these clips indeed name with "mc" and some number, but you are not sure the numbers are thorough from 1 to 50 without any gaps? Say, there's no "mc3"? The script above will form Array with null entries in it: [mc1, mc2, null, mc4, mc5, ... , mc50]. Not good. Still, there's another way to go.

// Define an empty Array.
var A:Array = new Array;

// A temporary variable to hold a name of the clip.
var aName:String;

// A temporary variable to check if name is right.
var anIndex:int;

// A temporary variable to refer your multiple clips one by one.
var anObject:DisplayObject;

// This loop iterates i over the number of display children
// in the current display container. It is to get each child
// with no regard of its name and to process it.
for (var i:int = 0; i < numChildren; i++)
{
    // This operation gets a child by its z-index.
    anObject = getChildAt(i);

    // Now we should figure if that child has a name that
    // falls into "mc" + some number schema.

    // Extract a portion of name from 2-nd character and on.
    aName = aChild.name.substr(2);

    // Convert it to the integer value.
    anIndex = int(aName);

    // Construct a proper name.
    aName = "mc" + anIndex;

    // This check will pass for names like "mc1" or "mc50"
    // but will fail for "mcx10", for example.
    if (aChild.name == aName)
    {
        // Put the found object into the Array.
        A.push(anObject);
    }
}

如果您在对片段编号时省略任何数字,则该原谅您,并且也不需要您实际对它们进行计数.缺点:它有点长,仍然需要为这些剪辑指定适当的实例名称.

This one will forgive you if you omit any numbers while numbering your clips, and also does not require of you to actually count them. The downside: it is a bit long, and still requires to give these clips proper instance names.

还有更好的方法.

如果所有这些剪辑都是同一个Library对象的实例,或者是有限数量的Library对象的实例,则可以为它们分配类名(以下示例通过2个类对其进行说明: MCNumA MCNumB ,还请记住,类名区分大小写),并检查类而不是实例名.好处是:您不必彻底命名50多个对象.

If all these clips are instances of the same Library object, or maybe of a limited number of Library objects, you can assign them class names (the example below explains it with 2 classes: MCNumA and MCNumB, also keep in mind that class names are case-sensitive) and check the classes instead of instance names. The upside: you don't have to thoroughly name 50+ objects.

import MCNumA;
import MCNumB;

// Define an empty Array.
var A:Array = new Array;

// Define a list of classes these objects belong to.
var classList:Array = [MCNumA, MCNumB];

// A temporary variable to iterate through class list.
var aClass:Class;

// A temporary variable to refer your multiple clips one by one.
var anObject:DisplayObject;

// This loop iterates i over the number of display children
// in the current display container. It is to get each child
// with no regard of its name and to process it.
for (var i:int = 0; i < numChildren; i++)
{
    // This operation gets a child by its z-index.
    anObject = getChildAt(i);

    // Iterate, get each class into aClass variable.
    for each (aClass in classList)
    {
        // Check, if this child belongs to any of the classes.
        if (anObject is aClass)
        {
            // Put the object into the Array.
            A.push(anObject);

            // Stop checking classes, we've found one already.
            // This breaks out of the inmost loop ("for each" one).
            break;
        }
    }
}

现在,以一种或另一种方式,我们有了要影响的对象的数组.其余的非常简单.

Now, one way or another, we have an Array of the objects you want to affect. The rest is pretty simple.

// Somewhere in your script.
if  (rightPressed)
{
    // When you have a list of them, it is THAT simple!
    // Iterate over all objects in A.
    for each (anObject in A)
    {
        // Give each object a proper X-coordinate.
        anObject.x = 5;
    }

如果您需要访问的基本功能较少,而这些功能超出了 DisplayObject 类的功能,则需要(对于 FlashPlayer )指定这些对象实际上属于哪个类.称为类型转换":

If you need to access less basic functionality, that exceeds the capabilities of DisplayObject class, you need to specify (for FlashPlayer) which class these objects actually belong to. It is called "type casting":

// Iterate over all objects in A.
for each (anObject in A)
{
    // Type casting. It will elevate the object's class
    // if it is really a MovieClip, or will return null.
    var aClip:MovieClip = anObject as MovieClip;

    // This check will return true if reference is not null.
    if (aClip)
    {
        // Stop the actual MovieClips.
        aClip.stop();
    }
}

如果您100%确信所有都是 MovieClip ,则可以忽略检查并使用正确键入的变量进行迭代:

If you are 100% sure all of them are MovieClip's, you are free to omit the check and iterate with the correctly typed variable:

// A temporary variable to iterate all the MovieClips.
var aClip:MovieClip;

// Iterate over all objects in A as MovieClips.
for each (aClip in A)
{
    // Stop the MovieClips.
    aClip.stop();
}

这篇关于在舞台as3上移动多个符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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