简体   繁体   English

AS3动态添加按钮来登台和使用其他功能

[英]AS3 Dynamically adding buttons to stage and accessing in different function

I am trying to add multiple different versions of the same button to the stage and to be able to access them later on by assigning them an ID. 我试图将同一按钮的多个不同版本添加到舞台上,以便以后可以通过为其分配ID来访问它们。 The way I assumed this would be done is to have a class for the button where an internal static variable is defined so that the ID can be found in the next function. 我认为这样做的方法是为按钮定义一个内部静态变量的类,以便可以在下一个函数中找到该ID。 This does not seem to be working, as the ID is constantly showing as the last number given, so in this case 6. 这似乎不起作用,因为ID一直显示为给出的最后一个数字,因此在这种情况下为6。

I assume that I am doing this wrong? 我以为我做错了吗? Here is my code so you can better understand: 这是我的代码,因此您可以更好地理解:

package src  {

import flash.display.MovieClip;
import flash.events.*;
import flash.utils.Timer;
import flash.net.*
import flash.display.Loader

public class main extends MovieClip {

public var xmlData:XML = new XML
public var currentName = null

    public function main() {
        var y = 0
        for(x=0; x<=6; x++){
            var names:namez = new namez
            namez.ID = y
            y++
            names.y = x*25
            names.addEventListener(MouseEvent.CLICK, checkMe)
            spinner.addChild(names)
        }
    }

    public function checkMe(e:MouseEvent){
        trace("clicked"+namez.ID)
    }
}
}

namez.as class: namez.as类:

package src {

import flash.display.MovieClip

public class namez extends MovieClip{

    internal static var ID

    public function namez() {
    }

}

}

Despite doing a trace for the buttons for namez.ID and it correctly showing 0,1,2,3,4,5,6 when the button is clicked on, the only number that shows is 6. 尽管对namez.ID的按钮进行了跟踪,并且在单击按钮时它正确显示了0、1、2、3、4、5、6,但唯一显示的数字是6。

How do I correctly go about making dynamically added buttons accessable by other functions? 如何正确使其他功能可以访问动态添加的按钮? Should I be using an array here? 我应该在这里使用数组吗?

FYI the total number of buttons required is 241. 仅供参考,所需的按钮总数为241。

Because ID is a static variable, that means there is only one ID for all instances of namez, whereas it sounds like you want an ID for each instance. 因为ID是一个静态变量,所以这意味着namez的所有实例只有一个ID,而听起来每个实例都需要一个ID。 Simply remove the 'static' attribute and define ID as follows: 只需删除“ static”属性并按如下所示定义ID:

internal var ID

Good catch by Pbirkoff on how you're accessing the .ID property, but you may also want to cast the event target to the namez class to access more custom properties. Pbirkoff很好地了解了如何访问.ID属性,但是您可能还希望将事件目标强制转换为namez类,以访问更多自定义属性。

I also note that you're missing some semi-colons and type definitions, which may or may not be required, but are good programming practice: 我还注意到,您缺少了一些分号和类型定义,它们可能需要也可能不需要,但它们是良好的编程习惯:

public function main():void {
    var y:int = 0
    for (x:int = 0; x<=6; x++) {
        var names:namez = new namez();
        namez.ID = y;
        y++;
        names.y = x*25;
        names.addEventListener(MouseEvent.CLICK, checkMe);
        spinner.addChild(names);
    }
}

private function checkMe(e:MouseEvent):void {
    var names:namez = e.target as namez;
    trace("clicked" + names.ID);
}

I think the problem lies in the fact that ID on namez is static. 我认为问题在于namez上的ID是静态的。 So you're not assigning the Id value to the particular instance, but to the namez-class. 因此,您不是将Id值分配给特定实例,而是分配给namez-class。

Make the variable a public property 将变量设为公共财产

public var Id:int;

Besides that, in your EventListener function checkMe, you're referencing the class namez again, not the instance. 除此之外,在EventListener函数checkMe中,您再次引用类namez,而不是实例。 Change the function into: 将该函数更改为:

public function checkMe(e:MouseEvent)
{
    trace('clicked: ' + e.target.Id);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM