Groups | Blog | Home
all groups > flash actionscript > april 2004 >

flash actionscript : Dynamically attaching buttons


Dinghus
4/6/2004 10:07:18 PM
Okay, I'm beating my head against the monitor screen. This is probably easy
but for some reason I can't do it.

I'm trying to just make square buttons with the letters of the alphabet on
them. Dynamically. So I created the square button blank. Named it myBtn.

Now I am trying to get it in the movie by attaching it. I get 1 copy of it
and no matter what I do with _x and _y it won't move from the upper left
corner.

I am beginning to suspect I can't attach buttons. Any insight into this would
be appreciated before my skull caves in.

letters = new
Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S
","T","U","V","W","X","Y","Z");
for(i=0;i<=letters.length-1;i++) {
attachMovie("myBtn","theBTN"+i,i,{_x:20*i + 10,_y:50});
createTextField("theLtr"+i,100+i,20*i+5,0,20,20);
theFormat = new TextFormat();
theFormat.bold = true;
this["theLtr"+i].type = "static";
this["theLtr"+i].text = letters[i];
this["theLtr"+i].setTextFormat(theFormat);
}
Jack.
4/6/2004 11:18:37 PM
for a movieclip instance your code will work, for a button
instance it fails on the initObject ( i don't know why ? )
a workaround to this, and my preference is to make a
reference to the new clip,

letters = [
"A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
for(var iv=0;iv!=letters.length;iv++) {
cRef = attachMovie("mybtn","mybtn"+iv,iv);
cRef._y = 50;
cRef._x = 20*iv+10;
cRef.onRollOver = function(){ getLetter(this) };

this.createTextField("theLtr"+iv,100+iv,20*iv+5,0,20,20);
tRef = this["theLtr"+iv];
tRef.selectable = false; //there is no type = "static";
tRef.text = letters[iv]; //values are: "dynamic" or "input"
theFormat = new TextFormat();
theFormat.bold = true;
tRef.setTextFormat(theFormat);
}
function getLetter(tgt){
var spl = tgt._name;
var arr = [];
arr = spl.split("mybtn");
trace("Letter "+letters[arr[1]]);
};

hth

Dinghus
4/7/2004 6:41:43 PM
Silly me. I thought the 3 choices were "dynamic","static", and "input". Just
like in the propety box. :\ Static DOES work tho. Or at least it doesn't NOT
work. Probably does nothing. LOL

Wonder why buttons break like that?

Anyway, this works great. I'm going to steal it and not give you credit.
Howzthat? Just kidding. I actually do put credit in my code when somebody
helps me with a tuff spot.
AddThis Social Bookmark Button