Actionscript 3 在阶段as3中移动多个符号

Actionscript 3 在阶段as3中移动多个符号,actionscript-3,Actionscript 3,我正在做一个简单的视频游戏,我需要在舞台上以同样的速度移动50多个符号。我想使用一个as3命令,通过它我可以同时瞄准所有符号。目前我已经逐一添加了符号: 用于将符号向右移动的代码段: stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_3); stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_3); function fl_MoveInDi

我正在做一个简单的视频游戏,我需要在舞台上以同样的速度移动50多个符号。我想使用一个as3命令,通过它我可以同时瞄准所有符号。目前我已经逐一添加了符号:

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

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符号


谢谢大家!

循环。当需要以相同的方式寻址多个对象时,可以使用循环。循环是对任何给定数量的对象(无论是2个、10个、100个或1000000个)执行相同操作过程的方法

首先,您需要一种方法来处理所有要处理的对象。当你说“循环”时,你通常会记住“数组”或“列表”。循环和数组相处得很好

最简单的方法是创建一个包含所有项的数组:

var A:Array = [mc1, mc2, mc3, /* and so on */ , mc50];
然而,它并没有什么美妙之处,而且如果你突然想将这些对象的数量从50个增加到150个,你将有很多枯燥的工作要做

幸运的是,您的剪辑命名方便,因此您可以通过构造它们的名称,然后通过方法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);
}
如果您需要访问的基本功能比DisplayObject类的功能少,则需要指定(对于FlashPlayer)这些对象实际属于哪个类。它被称为“类型铸造”:

如果您100%确定它们都是MovieClip,则可以省略检查并使用正确类型的变量进行迭代:

// 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();
}

嗨,我可以问另一个相关的问题吗?我使用数组代码进行移动,效果很好。非常感谢。现在我想将其用于“stop函数”:我当前的代码如下所示:mc1.stop();mc2.stop();mc3.stop();我试图用数组代码替换它:对于每个(A{anObject.stop()}中的anObject),我得到了这个错误:通过静态类型flash display:display object的引用调用一个可能未定义的方法。我怎样才能解决这个问题?感谢you@JudithHuber因为类具有层次结构。DisplayObject是可附加到显示列表的所有内容的基类。但是,只有MovieClip类具有stop()方法。我更新了我的帖子来思考这个问题。最后一个问题:)我正在用键盘箭头移动我的Mcs。一旦它们到达某个位置,我就把它们固定在一个固定的位置上。接下来,如果另一个MC也到达这个位置,我想将到达固定位置的MC移动到一个新位置。mc1到达固定位置y=100如果(mc2.y==100){mc1.y=400}如果(mc3.y==100){mc2.y=400 mc1.y=-1000}如果(mc4.y==100){mc3.y=400 mc2.y=-1000}我可以为此使用数组代码吗?类似这样的情况?:如果一个对象被另一个对象击中&如果其位置为y=100,则移动到y=400@JudithHuber当然您可以按顺序将mcs放入数组,即[mc1,mc2,…],然后使用for(var i:int=0;iimport 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; } } }
// 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;
    }
// 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();
    }
}
// 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();
}