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.
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:
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: