我正在制作一个在舞台上有多个MC实例的游戏(a1,b1,c1 ...... a2,b2,c2,d2等)
每次做一个听众我都要写
Object(this).sopa.c1.btn.addEventListener(MouseEvent.CLICK, c1Click);
并创建一个像这样的函数
function c1Click(event:MouseEvent):void
{
checkString += "c1";
var TFc1:TextFormat = Object(this).sopa.c1.letra.getTextFormat(0,1);
TFc1.color = 0xff0022;
Object(this).sopa.c1.letra.setTextFormat(TFc1);
check();
}
但是很多按钮(超过100个)要逐个长按它们。
所以我认为将按钮的值(实例名称)放在数组中。像这样的东西
var casillas:Array = [a1, b1,c1];
for (var i:uint = 0; i < casillas.length; i++)
{
casillas[i].addEventListener(MouseEvent.CLICK, a1presion);
trace(casillas[i])
}
但我不能得到数组元素的值只是一个[对象SimpleButton]。有任何想法如何做到这一点:我是as3的新人我一直在as2工作,这是很难理解的方式对我来说。
答案 0 :(得分:0)
这是面向对象编程的亮点。您将所有属性和方法封装到单个对象中。然后,您可以创建尽可能多的这些对象,并观察它们单独使用内部指定的行为。
这是一个我有时会自己使用的简单按钮类:
package buttons {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class SimpleButton extends MovieClip {
public function SimpleButton() {
addEventListener(MouseEvent.MOUSE_OVER, mOver);
addEventListener(MouseEvent.MOUSE_OUT, mOut);
addEventListener(MouseEvent.MOUSE_DOWN, mDown);
buttonMode = true;
}
protected function mOver(e:MouseEvent):void {
gotoAndStop(2);
}
protected function mOut(e:MouseEvent):void {
gotoAndStop(1);
}
protected function mDown(e:MouseEvent):void {}
}
}
然后根据需要创建任意数量的实例,并将它们存储在某个容器中,以便您可以在程序销毁的某个时刻摆脱它们。
var buttons:Vector.<SimpleButton> = new Vector.<SimpleButton>();
buttons.push(new SimpleButton());
buttons.push(new SimpleButton());
buttons.push(new SimpleButton());
答案 1 :(得分:0)
首先,我必须同意Iggy,如果使用面向对象的代码编写,那么您尝试做的最终将更易于理解和易于理解。
但是,根据您已经编写的内容,我认为您说的是您正在努力创建一个对所有按钮都通用的事件处理程序?因此,如果您需要获取MovieClip的字符串名称以及实际的剪辑引用,您可以执行以下操作:
var casillas:Array = ["a1", "b1", "c1"]; //Have an array of the names of the buttons
for (var i:uint = 0; i < casillas.length; i++)
{
var mcReference:MovieClip = this.sopa[ casillasNames[i] ]; //Use the string reference to get hold of the button
mcReference.btn.addEventListener(MouseEvent.CLICK, _handleButtonClick); //Add a listener to the same "btn" child of the casilla as you did before.
}
你需要一个听众
function _handleButtonClick(e:Event)
{
var buttonReference:* = e.target;
因此,此实例中的e.target应该是您之前添加到数组中的相同对象(a1或b1或c1,具体取决于单击哪一个)
现在我要对这些MovieClip(a1,b1,c1)的结构进行一些跳跃(我猜测它们在不同的数据中是完全相同的吗?)
function _handleButtonClick(event:MouseEvent):void
{
var clickedButtonReference:* = e.target;
for(var i:int = 0; i< casillas.length; i++)
{
if(this.sopa[ casillasNames[i] ].btn == clickedButtonReference) //Check each button on the stage to see if it is the one that has been clicked.
{
checkString += casillasNames[i];
//To find this bit "this.sopa.c1.letra" there are two options either
var selectedLetra = this.sopa[ casillasNames[i] ].letra; //Using the string name of the MC to find it.
//or
var selectedLetra = clickedButtonReference.parent.letra; //Or using the fact that we know that the original MC contains both the btn and the letra as children.
//Once we have it though the rest of your function should work ok.
var TFc1:TextFormat= selectedLetra.getTextFormat(0,1)
TFc1.color = 0xff0022;
selectedLetra.setTextFormat(TFc1);
check();
//Just add a break to stop searching for the button since we have found it.
break;
}
}
}