第一次在这里发帖。我使用一个循环来创建3个按钮,但我的问题是分配的按钮操作到每个按钮。我的按钮侦听/动作code仅适用于创建的最后一个按钮:(
我想由我给他们($ navButton.name =按钮+ I),但扔了一个未定义的错误为Button1的名称为目标的按钮。
任何帮助将是AP preciated,我是一个AS3小白:(
函数createNav(){
对于(VAR我:数量= 0; I< myXMLArray.length;我++)
{
$ navButton =新NavButton();
$ navButton.name =按钮+我;
$ navButton.x = I *(SIZE +间隔);
$ navButton.y = 179;
的addChild($ navButton);
跟踪($ navButton.name =+ $ navButton.name);
跟踪(按钮1 =+按钮1);
//试图给3个按钮的简单变换/点击动作:
$ navButton.buttonMode = $ navButton.buttonMode = TRUE;
$ navButton.addEventListener(侦听MouseEvent.MOUSE_UP,navAction);
$ navButton.addEventListener(MouseEvent.ROLL_OVER,函数(){$ navButton.nextFrame();});
$ navButton.addEventListener(MouseEvent.ROLL_OUT,函数(){$ navButton prevFrame();});
//以为我可以控制通过调用$ navButton.name
//button1.addEventListener(MouseEvent.MOUSE_UP,navAction);
//button1.addEventListener(MouseEvent.ROLL_OVER,函数(){button1.nextFrame();});
//button1.addEventListener(MouseEvent.ROLL_OUT,函数(){按钮1 prevFrame();});
/ * VAR traceText:文本字段=新的TextField();
traceText.defaultTextFormat = A12;
traceText.x = I *(SIZE +间隔);
traceText.y = 181;
traceText.width = 116;
traceText.height = 20;
traceText.text = myXMLArray [I] .ID;
的addChild(traceText); * /
}
}
功能navAction()
{
跟踪(点击);
}
解决方案
我想补充的按钮到一个数组广告获得这样:
在循环外部:
VAR按钮:数组= []
在循环:
buttons.push(navButton)
按钮[1] .addEventListener(侦听MouseEvent.MOUSE_UP,navAction);
本应在循环外做,如果你想自定义的功能添加到一个按钮。命名对象不给你一个对象,在实例的名称。要访问它,你有以上,你可能需要使用:
VAR按钮1:NavButton
这将允许您访问,你所做的一切。使用name属性,你可以使用的东西,如:
如果(navButton.name ==按钮1)
navButton.addEventListener(Event.BLAHBLAH,DoStuff);
我强烈建议不要使用匿名函数作为事件监听器。而是使用命名函数和事件的目标属性:
函数handleButtonClick(事件:MouseEvent)方法:无效
{
VAR按钮:NavButton将event.target =为NavButton;
如果(按钮)
button.nextFrame()
}
虽然它可能(可能会)的匿名函数的功能,这将是这么容易得多,如果你使用的命名函数进行调试。
顺便说一句,使用$您的增值经销商一般在AS3不好的形式。 这里的Flex SDK的标准,这是极好的约定所有的AS3编程。
first time posting here. I'm using a loop to create 3 buttons, but my problem is assigning the button actions to each button. My button listener/action code only works on the last button created :(
I tried to target buttons by the name that I gave them ($navButton.name = "button" + i) but that threw up an undefined error for button1.
Any help would be appreciated, I'm an AS3 noob :"(
function createNav(){
for (var i:Number = 0; i < myXMLArray.length; i++)
{
$navButton = new NavButton();
$navButton.name = "button" +i;
$navButton.x = i * (SIZE + SPACING);
$navButton.y = 179;
addChild($navButton);
trace("$navButton.name = "+$navButton.name);
trace("button 1 = "+button1);
//trying to give the 3 buttons a simple rollover/click action:
$navButton.buttonMode = $navButton.buttonMode = true;
$navButton.addEventListener(MouseEvent.MOUSE_UP, navAction);
$navButton.addEventListener(MouseEvent.ROLL_OVER, function() {$navButton.nextFrame();});
$navButton.addEventListener(MouseEvent.ROLL_OUT, function() {$navButton.prevFrame();});
//thought I could control via calling the $navButton.name
//button1.addEventListener(MouseEvent.MOUSE_UP, navAction);
//button1.addEventListener(MouseEvent.ROLL_OVER, function() {button1.nextFrame();});
//button1.addEventListener(MouseEvent.ROLL_OUT, function() {button1.prevFrame();});
/*var traceText:TextField = new TextField();
traceText.defaultTextFormat = a12;
traceText.x = i * (SIZE + SPACING);
traceText.y = 181;
traceText.width = 116;
traceText.height = 20;
traceText.text = myXMLArray[i].id;
addChild(traceText);*/
}
}
function navAction()
{
trace("click");
}
解决方案
I would add the buttons to an array ad access as such:
outside of the loop:
var buttons:Array = []
in the loop:
buttons.push(navButton)
buttons[1].addEventListener(MouseEvent.MOUSE_UP, navAction);
This should be done outside of the loop if you are trying to add custom functionality to a single button. Naming the object doesn't give you an object with the instance name. To access it as you have above, you'd need to use:
var button1:NavButton
which would allow you to access as you have done. Using the name property you could use something such as:
if(navButton.name == "button1")
navButton.addEventListener(Event.BLAHBLAH, DoStuff);
I strongly recommend against using anonymous functions as your event listeners. Instead use named functions and the target property of the event:
function handleButtonClick(event:MouseEvent):void
{
var button:NavButton = event.target as NavButton;
if(button)
button.nextFrame()
}
While it might (probably will) function with the anonymous functions, it will be so much easier to debug if you use named functions.
As an aside, using the $ for your vars is generally bad form in AS3. Here's the Flex SDK Standards, which are excellent conventions for all AS3 programming.