如何在AS3中使用变量编写对象名称以进行迭代? [英] How to write an object name with a variable for iteration in AS3?

查看:64
本文介绍了如何在AS3中使用变量编写对象名称以进行迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想用实例名称(例如"button_1"等)制作几个(或多个)按钮.没有一种方法可以通过遍历数字组件来遍历函数或事件处理程序中的所有实例实例名称的名称?

Let's say I wanted to make several (or more) buttons with instance names like "button_1" etc. Isn't there a way to loop through all of the instances in a function or an event handler by iterating through the number component of the instance name?

例如:

for (i = 1; i < 5; i++) {
    btn_[i].addEventListener(MouseEvent.CLICK, onBtn_[i]Click);
}

为了消除:

//btn_2.addEventListener(MouseEvent.CLICK, onBtn_2Click);
//btn_3.addEventListener(MouseEvent.CLICK, onBtn_3Click);
//btn_4.addEventListener(MouseEvent.CLICK, onBtn_4Click);
//btn_5.addEventListener(MouseEvent.CLICK, onBtn_5Click);  

感谢您可以提供的任何帮助.

Thanks for any help you can offer.

我认为,通过使事件侦听器返回事件的目标,还有更好的方法吗?但是我现在并不太担心,只是想知道是否专门遍历对象实例.

I think there is a better way to do this by making the event listener refer back to the target of the event? But I'm not too worried about that for now, just wondering about iterating through object instances specifically.

哦,我发布的代码抛出了这个问题:

Oh, and my code that I posted throws this:

1084: Syntax error: expecting rightparen before Click.

谢谢,全部!

推荐答案

您想要什么

我想使用实例名称(例如"button_1")制作几个(或多个)按钮

I wanted to make several (or more) buttons with instance names like "button_1"

不,您真的不想这样做,是

No, you really don't want to do that, it's

  • 乏味:如果您需要50个按钮怎么办?您真的要手工命名吗?
  • 容易出错:如果您在某处打错了怎么办?否则,您在复制&粘贴名称的通用部分?如果您忘记号码或使用两次,该怎么办?
  • 不易维护:如果要删除按钮3到7,该怎么办?您如何处理这一数字鸿沟?如果要在中间包括2个按钮怎么办?您是否重命名以下所有按钮?

实例名称的数字部分?

the number component of the instance name?

使用名称来标识特定的单个对象非常有用,但是如果列表应详尽无遗,则列出单个名称不是理想的解决方案.

Names are great to identify specific single objects, but listing individual names is not an ideal solution if the list should be exhaustive.

如果您的兄弟是杰克,山姆和罗宾,您会在演讲中提到他们吗?还是只说我的兄弟?

If your brothers are Jack, Sam and Robin would you refer to them with that list in speech or would you just say my brothers?

没有办法遍历所有实例

Isn't there a way to loop through all of the instances

这是实际需要的东西.您有几个对象,并且需要某种机制来对它们进行分组.

This is the actual desirable thing. You have several objects and you need some mechanism to group them.

有几种这样的机制可以结合使用:

There are several such mechanisms that can be used in conjunction:

数组只是事物的列表.所有按钮的列表是您要寻找的分组机制.将所有按钮和功能包含在数组中可以让您遍历数组:

An array is just a list of things. A list of all the buttons is the grouping mechanism you are looking for. Having all buttons and functions in arrays allows you to iterate over the arrays:

var buttons:Array = [btn_1, btn_2];
var functions:Array = [onBtn_1Click, onBtn_2Click];

这将允许您做与您要求的非常相似的事情:

Which would allow you to do something very similar to what you asked for:

for (var i:int = 1; i < buttons.length; i++) {
    buttons[i].addEventListener(MouseEvent.CLICK, functions[i]);
}

这仍然有一些问题:

  • 按钮和功能都必须明确命名
  • 手动填充数组实际上会增加另一层复杂性,这似乎是多余的,并且容易出错:如果添加按钮但忘记将其添加到数组中怎么办?

我们必须进一步寻找.

等等! []括号是一个很酷的技巧,可让您组合名称和内容.

嗯,确实有可能使用所述括号动态访问对象的属性.

Well, there is indeed the possibility to access properties of objects dynamically with said brackets.

如果您的对象具有属性:

If you have an object with a property:

var object:Object = {property:"hello world"};

您有两种访问此属性的可能性.点语法:

You have two possibilities to access this property. Dot syntax:

trace(object.property);

和括号:

trace(object["property"]);

使用方括号,可以动态选择属性名称:

With the brackets, the property name can be chosen dynamically:

var object:Object = {property:"hello world"};
var propertyName:String = "property";
trace(object[propertyName]);

因此可以以任何方式组装:

and thus can be assembled in any way:

var object:Object = {property:"hello world"};
var propertyName:String = "prop";
propertyName += "erty";
trace(object[propertyName]);

由于实例名称已转换为时间轴的属性名称,因此您可以通过以下方式访问实例:

Because instance names are turned into property names of the time line, you can access your instances this way:

this["button_" + 1]

现在,您可以轻松地填充数组,例如,说出其中一个按钮:

Now you can easily fill the arrays, say the one of the buttons for example:

var buttons:Array = []; //creating an empty array

for (var i:int = 1; i < 5; i++) {
    buttons.push(this["button_" + i]);
}

问题在于它不能解决实际问题.它只是使可笑的任务变得不那么繁琐.

您仍然必须为按钮键入所有这些单独的名称.没有什么可以阻止您输入错误的名称,两次输入的名称或跳过值.

You still have to type all those individual names for the buttons. Nothing prevents you from typing a wrong name, a name twice or to skip a value.

您将在整个Internet上找到此解决方案",但这更多是一种快速而肮脏的hack,看起来似乎很聪明,但对解决根本问题并没有真正的帮助.

You will find this "solution" all over the internet, but it is more of a quick and dirty hack that only appears to be clever but doesn't really help with the underlying problem.

显示所有按钮.这意味着它们在显示列表中.您可以使用代码访问和操作该列表.

All the buttons are displayed. That means they are on the display list. You can access and manipulate that list with code.

这听起来令人怀疑,之前有一个Array东西也是一个列表,它并没有真正解决问题.

This sounds dubious, there was this Array thing before which was also a list and it didn't really solve the problem.

不同之处在于按钮会自动显示在显示列表中.您无需做任何事情. 您不必使用名称来标识它们.

The difference is that the buttons are on the display list automatically. You don't have to do anything. You do not have to identify them with names.

显示列表由类似 MovieClip . 每个容器可以包含其他容器,这些容器又可以包含容器等.

The display list is made up of containers like MovieClip. Each container can contain other containers, which in turn can contain containers, etc.

这是怎么回事:

  1. 在时间轴上创建一个空的MovieClip,并为其指定实例名称buttonContainer.
  2. 将所有按钮添加到该buttonContainer.这些按钮不需要具有实例名称!
  1. Create an empty MovieClip on the timeline and give it an instance name buttonContainer.
  2. Add all your buttons to that buttonContainer. The buttons do not need to have instance names!

有点像一个数组,但是您可以直观地对其进行组装,不需要单独的实例名称,只需一个容器名称即可.

It's kind of like an array, but you assemble it visually and you do not need individual instance names, just one for the container.

该容器还知道其中有多少东西.要遍历它们,您也可以使用for循环.这是使按钮半透明的示例:

The container also knows how many things are inside it. To iterate over them all you can use a for loop, too. Here's an example that makes the buttons half transparent:

for (var i:int = 0; i < buttonContainer.numChildren; i++) {
        buttonContainer.getChildAt(i).alpha = 0.5;
}

此代码几乎可用于该容器中的任意数量的按钮. 您要做的就是确保将按钮添加到容器中.

This code works for pretty much any number of buttons in that container. All you have to do is make sure you add the buttons to the container.

好的,现在我该如何使用这些功能?

这是问题所在:您不能.没有内置的机制可以将功能与显示对象一起分组.

This is where the problem is: you cannot. There's no built in mechanism that groups functions together the way it happens with display objects.

我认为通过使事件侦听器返回事件的目标可以找到一种更好的方法吗?

I think there is a better way to do this by making the event listener refer back to the target of the event?

确实存在,这是解决每个按钮具有单独功能的问题的原因.取而代之的是,只有一个与事件源一起使用的函数:currentTarget.这样的功能看起来像是半透明:

Indeed there is and it's what solves the problem of having individual functions for each button. Instead, have only one function that works with the source of the event: currentTarget. Here's what such a function would look like that goes to half transparency:

function onClick(e:MouseEvent):void
{
    currentTarget.alpha = 0.5;
}

您现在可以将此功能添加到所有按钮:

You can now add this function to all buttons:

for (var i:int = 0; i < buttonContainer.numChildren; i++) {
        buttonContainer.getChildAt(i).addEventListener(MouseEvent.CLICK, onClick);
}

就是这样!简单明了:将此侦听器添加到该容器中的所有按钮.没有名字巫术.只是一个容器.

That's it! Plain and simple: add this listener to all the buttons in that container. No name wizardry. Just a container.

老兄!您在上面的所有琐事上都度过了美好的时光,推出了几乎一个解决方案,另一个解决方案,只有最后一个实际上完成了工作.现在这个俗气的字幕怎么了?

以上所有假设均假定列表中具有所有按钮是实际目标,并且确实可以解决问题.但是,如果您考虑一下,上面的所有内容都将尽可能地巧妙地将各个按钮放在列表中. this["button_" + i]有其缺陷,而DisplayObjectContainer解决方案已修复它们,但又引入了自己的缺陷:如果由于某种原因不能将所有按钮添加到容器中怎么办?如果有的装在一个容器中,有的装在另一个容器中怎么办?

All of the above assumes that having all buttons on a list is the actual goal and indeed it solved the problem. But if you think about it, everything above is just trying to be as clever as possible about how to put the individual buttons on the list. this["button_" + i] has its flaws and the DisplayObjectContainer solutions fixed them, but introduced its own ones: What if you cannot add all buttons to a container for some reason? What if some are in one container and some are in another?

您可以组合使用这些方法,并将多个容器中的按钮添加到单个数组中,如下所示:

You could combine the approaches and add the buttons from several containers into a single array, like so:

var buttons:Array = [];

for (var i:int = 0; i < containerA.numChildren; i++) {
        buttons(containerA.getChildAt(i));
}

for (i = 0; i < containerB.numChildren; i++) {
        buttons(containerB.getChildAt(i));
}

该数组是独立于显示列表的超级容器. 这仍然糟透了.如果按钮四处分散并且无法按容器分组怎么办?

The array is the super container that's independent from the display list. This still sucks. What if the buttons are scattered around and cannot be grouped by containers?

您还记得杰克,山姆和罗宾吗?以及将他们称为我的兄弟"作为他们名字的列表,会更容易吗?

Do you remember Jack, Sam and Robin? And how it was easier to refer to them as "my brothers" as a kind of list of their names?

如果您想提及世界上所有兄弟怎么办? 这似乎是一个荒谬的任务,但实际上所有兄弟"一词确实做到了这一点.它没有说有多少,也没有在哪里,但可以肯定的是它们指的是它们.这样做不是全部创建一个明确的列表,而是,但是它们符合兄弟是什么的定义:男性兄弟姐妹.

What if you wanted to refer to all brothers in the world? It seems to be a ridiculous task, but actually the phrase "all brothers" does exactly that. It doesn't say how many there are or where they are, but sure enough it refers to them. It does so not by creating an explicit list of them all, but them fitting to the definition of what a brother is: a male sibling.

您实际上也不需要所有按钮的明确列表.您只想通过定义按钮的含义来隐式地引用存在的所有按钮.

You don't really need an explicit list of all buttons either. You just want to refer to all the buttons in existence implicitly by having a definition of what a button is.

一个类就是这样的定义.定义名称为按钮并从该定义派生对象,而不是通过名称,容器或您想象的任何其他方式来识别按钮的对象.

A class is such a definition. Instead of running around identifying objects that are buttons, either by name, container or whatever other way you imagined, define what a button is and derive objects from that definition.

这是文件TransparentButton.as的样子:

Here's what a file TransparentButton.as could look like:

package
{
    public class TransparentButton extends SimpleButton
    {
        public function TransparentButton()
        {
            addEventListener(MouseEvent.CLICK, onClick);
        }

        private function onClick(e:MouseEvent):void
        {
            this.alpha = 0.5;
        }
    }
}

从本质上讲,这定义了创建按钮时,它会向其自身添加一个侦听器.该功能将其透明度降低到0.5.

Essentially, this defines that when a button is created, it adds a listener to itself. That function reduces its transparency to 0.5.

如果您现在将该类与库中的按钮符号关联,则将导致您拖动&放到时间表上以按照该类定义进行操作.

If you now associate that class with your button symbol in the library, it will cause the button instances that you drag & drop onto the time line to behave according to that class definition.

免责声明:此类不一定能按原样工作,具体取决于按钮的符号类型(如果它们实际上是MovieClips,则将失败).也许它缺少一些进口.

disclaimer: This class does not necessarily work as-is, depending on what type of symbol your buttons are (if they are actually MovieClips this will fail). Maybe it lacks some imports.

这篇关于如何在AS3中使用变量编写对象名称以进行迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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