Kevin McGowan
Kevin McGowan

Reputation: 463

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. 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. This does not seem to be working, as the ID is constantly showing as the last number given, so in this case 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:

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.

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.

Upvotes: 0

Views: 1853

Answers (2)

Jeff Ward
Jeff Ward

Reputation: 19006

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. Simply remove the 'static' attribute and define ID as follows:

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.

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);
}

Upvotes: 2

Pbirkoff
Pbirkoff

Reputation: 4702

I think the problem lies in the fact that ID on namez is static. So you're not assigning the Id value to the particular instance, but to the 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. Change the function into:

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

Upvotes: 1

Related Questions