PDA

View Full Version : assigning MC instance name to generic variable?



lunatic
December 6th, 2003, 09:39 PM
Howdy,

I'm pretty sure there is a simple solution to this problem and I'm going to feel really dumb for not figuring it out myself but for some reason I am stuck so here goes.

I have several movie clip "buttons". I would like, on release, to load a dynamic text file into a container clip on the stage. Pretty simple right? Here is the code I am using:


buttonMC1.onRelease = function() {
loadText = new loadVars();
loadText.load("sampletext.txt");
loadText.onLoad = function(success) {
if (success) {
container.scroller.text = this.mstxt;
}
};
};



What I'[d like to do is have a generic variable for the "buttonMC1" and for the "sampletext.txt". Then, depending on what button is pressed there would be code on that button that says, essentially,

buttonMC1 = specific instnace name
sampletext = specific text file

I'm sure this is pretty common. I've searched the forums but it's hard to know what to search for?

Thanks all for any help! :D

norie
December 6th, 2003, 11:54 PM
obj = "buttonMC1";
filename = "sampletext.txt"
this[obj].onRelease = function() {
loadText = new loadVars();
loadText.load(filename);
loadText.onLoad = function(success) {
if (success) {
container.scroller.text = this.mstxt;
}
};
};

lunatic
December 7th, 2003, 12:02 AM
Hey Norie, thanks!

question though - do I put


obj = "buttonMC1";
filename = "sampletext.txt"


on the button(s) and the rest on the main timeline? (repeat same for each button)?

:ear:

norie
December 7th, 2003, 01:07 AM
i guess the more logical thing to do would to make a function, put this on the main timeline:


function textLoad(filename) {
loadText = new LoadVars();
loadText.load(filename);
loadText.onLoad = function(success) {
if (success) {
container.scroller.text = this.mstxt;
}
};
}
and assign the button fuctions like this (still on the main timeline):


buttonMC1.onRelease = function(){
_root.textLoad("poo.txt");
}

or if you have LOTS of buttons you can do this (main timeline):


for (i = 0; i < 10; i++) {
this["buttonMC" + i].onRelease = function() {
_root.textLoad("poo.txt");
};
}

lunatic
December 7th, 2003, 12:31 PM
Thanks! That works great!

Except I seem to be having a path problem. I have an array for my text files so that my code now looks like this:


function textLoad(filename) {
loadText = new LoadVars();
loadText.load(filename);
loadText.onLoad = function(success) {
if (success) {
container.scroller.text = this.mstxt;
}
};
}

for (i = 0; i < 10; i++) {
this.mcs[i].onRelease = function() {
container._visible = true;
_root.textLoad(txts[i]);
};
}

where "txts[]" is the textfile array.

But it just balks at that. How can I load a specific spot in the array depending on what button is pushed?

:h:

Thanks for any help!

norie
December 7th, 2003, 03:42 PM
i'm not quite sure what you mean.

lunatic
December 7th, 2003, 10:58 PM
When I press a button I want it to load a text file into the dynamic text box on "container" MC. When I press another button I want it to load a different text file into the dynamic text box on "container" MC. There are 10 buttons and 10 text files. I was hoping to use one load function with the onRelease actionscript. The code you suggested is great for setting the onRelease and popping up the containerMC but I can't figure out to make it work for the text files. The code that I tried (above) returns a "cannot find file" error message. If I do a trace on i it just returns 10 because with the for loop it loads all of them instead of one per button.

I was thinking originally that I could use a generic variable for the function and then maybe put some AS on each button that assigns it's value to the generic variable. Two generic variables actually - one for the button that is being 'onRelease'd and the other for the text file. So it would look something like:


genericMCbuttonvar.onRelease = function() {
loadText = new loadVars();
loadText.load(generictxtvar);
loadText.onLoad = function(success) {
if (success) {
container.scroller.text = this.mstxt;
}
};
};


and on a button (for example):


genericMCbuttonvar = thisInstanceName;
generictxtvar = "thistextfile.txt"


Does that makes sense? :h:

Is that possible?

norie
December 8th, 2003, 01:40 AM
i just threw an fla together. check this out:

lunatic
December 8th, 2003, 09:41 PM
Ahhhhh! That's BRILLIANT! :thumb: Thank you sooooooo much! That's much simpler than I thought!

Just one question - what is "d"? As in:


this["button" + i].d = i;


:h:

p.s. your footer is awesome

lunatic
December 8th, 2003, 09:55 PM
:*(

I'm sorry, I'm not sure what I'm doing wrong? I can't get any text to load. Here is what I have:


function textLoad(filename) {
loadText = new LoadVars();
loadText.load(filename);
loadText.onLoad = function(success) {
if (success) {
_root.container.scroller.text = this.mstext;
}
};
}
for (i = 1; i < 11; i++) {
this["button" + i].d = i;
this["button" + i].onRelease = function() {
container._visible = true;
_root.textLoad("subt/text" + this.d + ".txt");
trace(i);
};
}


where subt is the subfolder the textfiles are in. On my main stage I have a container mc. On the container I have a dynamic text box and scroller.

The trace just keeps returning 11 . . .

norie
December 9th, 2003, 12:56 AM
'i' is supposed to be 11. Think of 'onRelease' as a variable. That variable tells the button what action to take when it is released. the action is only enacted when the button is released. the string that describes that action is:

root.textLoad("subt/text" + this.d + ".txt");
On the other hand the 'for' loop is only ran ONCE. so before the loop is ran 'i' is undefined once the loop is finished 'i' remains 11. so if you trace 'i' inside of the 'onRelease' action script it will always return 11. so if you define another variable 'd' to be equal to 'i' DURING the loop, 'd' will equal whatever 'i' was at that current point and not 'i' at the end of the loop. using the following syntax makes the variable 'd' relative to the movieclip so that each one is unique:

myInstanceName.d = i;
for example, this:

//when i = 8
root.textLoad("subt/text" + this.d + ".txt");
will execute like this:

root.textLoad("subt/text8.txt");

i'm not sure what your problem is; it must be paths b/c the script works when applied correctly. If you send/post the fla i can take a look at it.

lunatic
December 9th, 2003, 01:10 AM
Great explanation - thanks.

Can I email you the file? I'd rather not post it just yet.

Thanks Norie for all your time and help. Sorry to be such a n00b!

norie
December 9th, 2003, 05:11 PM
*lol*

change this:


function textLoad(filename) {
loadText = new LoadVars();
loadText.load(filename);
loadText.onLoad = function(success) {
if (success) {
container.scroller.text = this.mstext;
}
};
}

to this:


function textLoad(filename) {
loadText = new LoadVars();
loadText.load(filename);
loadText.onLoad = function(success) {
if (success) {
container.scroller.text = this.mstxt;
}
};
}

and go to your scroller text box and unclick the '<>' -- html on button. and it SHOULD work.

lunatic
December 9th, 2003, 05:24 PM
I'm at work right now :d: so can't make changes until I get home. But out of curiosity - what's the difference between the 1st and 2nd bits of code you have here? :h:

norie
December 9th, 2003, 05:30 PM
container.scroller.text = this.mstext;
container.scroller.text = this.mstxt;

in the text files the variable is mstxt, not mstext. :beam:
and i don't know why you need to unclick the '<>' HTML on button, but it just doesn't work with it on. Plus i don't think you need HTML on b/c all your files seem to be plain text anywayse

lunatic
December 9th, 2003, 05:44 PM
:sigh: Where's that dang trout? Oh wait here it is: :trout: (sound of smacking herself). I can't believe I'm such a nimrod. Sheesh. Thanks. No wait, THANK YOU SO MUCH!!!! :love:

Weird about the html render. The reason I did that is because the REAL text that I will put in there eventually might include links or tags. Guess I'll tackle that when I need to.

Thanks again Norie! You rule! :P