在 as3 阶段移动多个符号
moving multiple symbols across stage as3
我正在开发一个简单的视频游戏,我需要以相同的速度在舞台上移动 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 符号?
谢谢!
循环。当您需要以相同方式处理多个对象时,您可以使用 loops。循环是对任何(字面上任何)给定数量的对象执行相同操作过程的方法,无论是 2、10、100 还是 1000000。
首先,您需要一种方法来处理您将要使用的所有对象。当您说“loop”时,您通常会记住“array”或“list”。循环和数组相处得很好。
最直接的方法是创建一个包含所有项目的数组:
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);
}
现在,如果你想从 50 上升到 150,只需将 i <= 50 更改为 i <= 150 即可它。
那么,如果这些片段确实以"mc"和一些数字命名,但您不确定这些数字是否从1到50没有任何差距?话说,没有"mc3"?上面的脚本将形成 Array,其中包含 null 个条目:[mc1, mc2, null, mc4, mc5, ... , mc50]。不好。不过,还有另一条路要走。
// 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);
}
}
如果您在给剪辑编号时遗漏了任何数字,这将原谅您,并且不需要您实际计算它们。缺点:有点长,而且还需要给这些片段适当的实例名称。
还有更好的方法。
如果所有这些剪辑都是同一个库对象的实例,或者可能是有限数量的库对象的实例,您可以为它们分配 class 名称(下面的示例用 2 class es: MCNumA 和 MCNumb,还要记住 class 名称区分大小写)并检查 class es 而不是实例名称。好处:您不必彻底命名 50 多个对象。
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;
}
}
}
现在,不管怎样,我们有一个 Array 要影响的对象。剩下的就很简单了。
// 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 class 的功能,您需要指定(对于 FlashPlayer) class 这些对象实际上属于哪个。它被称为 "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 的,您可以随意省略检查并使用正确键入的变量进行迭代:
// 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();
}
我正在开发一个简单的视频游戏,我需要以相同的速度在舞台上移动 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 符号?
谢谢!
循环。当您需要以相同方式处理多个对象时,您可以使用 loops。循环是对任何(字面上任何)给定数量的对象执行相同操作过程的方法,无论是 2、10、100 还是 1000000。
首先,您需要一种方法来处理您将要使用的所有对象。当您说“loop”时,您通常会记住“array”或“list”。循环和数组相处得很好。
最直接的方法是创建一个包含所有项目的数组:
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);
}
现在,如果你想从 50 上升到 150,只需将 i <= 50 更改为 i <= 150 即可它。
那么,如果这些片段确实以"mc"和一些数字命名,但您不确定这些数字是否从1到50没有任何差距?话说,没有"mc3"?上面的脚本将形成 Array,其中包含 null 个条目:[mc1, mc2, null, mc4, mc5, ... , mc50]。不好。不过,还有另一条路要走。
// 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);
}
}
如果您在给剪辑编号时遗漏了任何数字,这将原谅您,并且不需要您实际计算它们。缺点:有点长,而且还需要给这些片段适当的实例名称。
还有更好的方法。
如果所有这些剪辑都是同一个库对象的实例,或者可能是有限数量的库对象的实例,您可以为它们分配 class 名称(下面的示例用 2 class es: MCNumA 和 MCNumb,还要记住 class 名称区分大小写)并检查 class es 而不是实例名称。好处:您不必彻底命名 50 多个对象。
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;
}
}
}
现在,不管怎样,我们有一个 Array 要影响的对象。剩下的就很简单了。
// 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 class 的功能,您需要指定(对于 FlashPlayer) class 这些对象实际上属于哪个。它被称为 "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 的,您可以随意省略检查并使用正确键入的变量进行迭代:
// 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();
}