PDA

View Full Version : Dynamically created buttons from txt file


montey
03-11-2003, 05:15 PM
Ok i'm loading a txt file with this info in it.

tammy=jes,dean,fran,oct,simmy,blanch,drean

&talk=bright,dark,canning,prut,stenk,blast

&drift=tt,ba,br

I am using the load vars()

function LoadArr(file) {
arr_obj = new LoadVars();
arr_obj.load(file);
arr_obj.onLoad = function(success) {
if (success) {
buttons = new Array();
buttons.push(arr_obj);
trace(buttons);
} else {
trace("not loaded");
}
};
}
loadArr("arr.txt");

Now what I want to do is to dynamically create buttons from the variable names
tammy
talk
drift
then from the buttons I need to access the values of these variables.
[code]
tammy.onRelease=function(){
for (var i=0; i<tammy.length; i++) {
arr=tammy[i];

}
How in the hell can this be done.

jaybee
03-11-2003, 07:15 PM
if tammy talk and drift will always be the same you can do it like this, and you have a blank button mc in the library with a textfield nested within it:


function loadArr(file) {
arr_obj = new LoadVars();
arr_obj.tl = this;
arr_obj.onLoad = function(success) {
if (success) {
var myArray = new Array("tammy", "talk", "drift");
var depth = 2;
for (var i = 0; i<myArray.length; i++) {
var n = myArray[i];
this.tl[n+"_arr"] = this[n].split(",");
myInit._x *= i;
var t = this.tl.attachMovie("button_mc", n+"_btn", depth++, {_x:150*i,_y:20});
t.arrayName = t.name_txt.text=n;
t.onRelease = function() {
var arr = _root[this.arrayName+"_arr"];
for (var i = 0; i<arr.length; i++) {
trace(arr[i]);
}
};
}
} else {
trace("not loaded");
}
};
arr_obj.load(file);
}
loadArr("buttons.txt");


I tested this out in a fla and have attached it here - is this what you wanted?

montey
03-11-2003, 07:23 PM
The problem is I don't want to have to declare these variables in flash the should be in the txt file which they are with a value on them

var myArray = new Array("tammy", "talk", "drift");


These will change they may not be called these names and they might have different values.
I need to have them declared in the txt file like they are, then loaded into flash, then they are created into buttons and then these buttons can access there value.
Just like you have done except the var myArray = new Array("tammy", "talk", "drift");
has to be loaded in from a text file.
Thanks for your reply

jaybee
03-11-2003, 07:32 PM
ok then, replace the line


var myArray = new Array("tammy", "talk", "drift");



var myArray = new Array();
for(var i in this) {
if (i != "tl" && i != "onLoad") myArray.push(i);
}



which works but the contents of thefor loop are an ugly hack - can't remember how this would be done with instanceof or typeof at the mo...it works tho.

montey
03-11-2003, 07:40 PM
This works. I am trying to go through it all and I may have a few questions later.
one is why this
arr_obj.tl = this;
the .tl
part....
Thanks so much for the reply and the amazing answer.

jaybee
03-12-2003, 08:53 AM
tl is a reference to the parent timeline that the loadvars object is on, cos the code in the loadhandler function is executed relative to the loadvars scope....so then you can say this.tl.someFunction() quite neatly.