all groups > flash actionscript > april 2005 >
You're in the

flash actionscript

group:

Loading External Text & Variables



Loading External Text & Variables Patrick_AK
4/27/2005 12:00:00 AM
flash actionscript: I am hoping that someone can help me with this problem.

I would like to load some external text into a dynamic text box. Easy? Sure.

BUT.... I'd like to load different text using variables. (see second section
of code)
The method that I'm using obviously will not work but I'm hoping that someone
sees what I'm trying to do. I'm not the smartest when it comes to AS but I'm
learning! :) Any suggestions are greatly appreciated. Thanks.



var myLV:LoadVars = new LoadVars();

myLV.onLoad = function (success) {
if (success) {
_root.textBox.text = myLV.info;
}
}
_root.myLV.load("text.txt");

//----------Loading text using another variable----------\\
var myLV:LoadVars = new LoadVars();

var vText:String = new String();

vText = "_root.textBox.text = myLV.info";

myLV.onLoad = function (success) {
if (success) {
vText;
}
}
_root.myLV.load("text.txt");
Re: Loading External Text & Variables NSurveyor
4/27/2005 12:00:00 AM
Re: Loading External Text & Variables Patrick_AK
4/27/2005 12:00:00 AM
Let's see if I can simplify what I'm trying to do so I don't confuse us both. :)

I have 6 buttons which will each load text dynamically, apply a CSS file, and
load a dynamic image. There is one dynamic text field and one empty movieclip
which will hold this information. Now, I'd much rather create a generic
function for all six of those buttons and use a variable for the text and a
variable for the picture to fill in the details within each ".onRelease".

The information (text) which will be placed into the dynamic text field is in
6 different .txt files. Each button has its own .txt file to load.

Is this even possible?

p.s. thank you for your interest in helping.
Re: Loading External Text & Variables NSurveyor
4/27/2005 12:00:00 AM
Something like this? Put the textfiles in the array, files. And the path to
your buttons in the btns array.

files = ['1.txt','2.txt','3.txt','4.txt','5.txt','6.txt'];
btns = [but_1,but_2,but_3,but_4,but_5,but6];
for(j=0;j<btns.length;j++){
cbtn = btns[j];
cbtn.j = j;
cbtn.onRelease = function(){
loadTheFile(files[this.j]);
}
}

function loadTheFile(file){
_root.textBox.text = 'Loading...';
lv = new LoadVars();
lv.onLoad = function(s){
if(s){
_root.textBox.text = this.info;
}
}
lv.load(file);
}
Re: Loading External Text & Variables Patrick_AK
4/27/2005 12:00:00 AM
WOW!!! Thanks!! That's some sweet code there. I don't have any experience with
Arrays but after studying your code I've come to the realization that I have
some homework to do. Thanks for pointing me in the right direction! :)
Re: Loading External Text & Variables NSurveyor
4/28/2005 12:00:00 AM
Re: Loading External Text & Variables Patrick_AK
4/28/2005 12:00:00 AM
Ok. I've been checkin out the code and it's a work of art compared to stuff I
come up with but I was wondering if you might be able to explain how the code
works. Staring at it for an hour just doesn't seem to reveal anything new.
:confused;

I understand the Arrays and the For Statement but right about where the
"cbtn.j=j" is where I start to get lost. (lame... i know)

Thanks again. :beer;
Re: Loading External Text & Variables NSurveyor
4/28/2005 12:00:00 AM
1 files = ['1.txt','2.txt','3.txt','4.txt','5.txt','6.txt'];
2 btns = [but_1,but_2,but_3,but_4,but_5,but6];
3 for(j=0;j<btns.length;j++){
4 cbtn = btns[j];
5 cbtn.j = j;
6 cbtn.onRelease = function(){
7 loadTheFile(files[this.j]);
8 }
9 }

Ok. So Lines 1 and 2 are the arrays. 3 we call a for loop where we have a
variable, j that starts out as 0 until it hits the number of items in the
arrays. On line 4 we make a variable to hold the button we are working with.
Each time we go through the loop, the cbtn variable will have a reference to a
different button, namely in the order we have in our array. On line 5, we make
a variable j (not the same j we used in the loop) that only belongs to the
current button itself that has a value of j (the for loop one). This is just so
the button "knows" which item it is in the array. So, in the 3rd iteration,
but_3 will have a variable j, with the value of 2 (arrays are 0 based, so it's
0, 1, 2 (<-- 3rd element). On line 6 we make a onRelease handler for the
current button. On line 7 we loadTheFile that corresponds to the button. We can
do this by looking at the file with the same position in the array as the
button. And the button already knows it's position because we made a variable
for it (the button's j). We can use this.j because the onRelease belongs to the
current button so this.j means cbtn.j. Line 8 closes the onRelase handler. Line
9 closes the for loop.

I hope that helps you understand. If you still don't understand something,
just let me know.
Re: Loading External Text & Variables Patrick_AK
4/29/2005 12:00:00 AM
Very nice :)

I finally understand! It's one thing to know the code works but it's another
thing to understand HOW it works. I really appreciate you taking the time to
explain it. And you explained it VERY well. Do you teach ActionScript or write
any tutorials?

Anyways, thank you again.
Re: Loading External Text & Variables NSurveyor
4/29/2005 12:00:00 AM
AddThis Social Bookmark Button