the title pretty much explains. how do i load text from a .txt file into a dynamic text box.
Printable View
the title pretty much explains. how do i load text from a .txt file into a dynamic text box.
IMHO- the best way to do this is using a varObject. Heres an example:
code:
newText = new LoadVars();
newText.onLoad = function(success) {
if (!success) {
someTextField_txt.htmlText = true;
someTextField_txt.htmlText = "<b>FAILED TO LOAD</b>"
trace("failed to load");
} else {
//do whatever you want to happen IF successful
someTextField_txt.htmlText = true;
someTextField_txt.htmlText = (newText.info); //info = variable in text file
}
};
newText.load("someTextFile.txt");
}
this example assumes that inside the textFile you have the "data" set up as:
explaination of how it works:Quote:
&info=etc..etc.....and some text..etc..etc...more text...etc.
newText = new LoadVars();
this declares/creates a NEW loadVar Object (we called it 'newText')
newText.onLoad = function(success) {
we are telling FLASH what to do when the textFile is loaded..
- if (!success) {
someTextField_txt.htmlText = true;
someTextField_txt.htmlText = "<b>FAILED TO LOAD</b>"
trace("failed to load");
}
this section is telling FLASH what to do if text DID NOT LOAD CORRECTLY.
- else {
//do whatever you want to happen IF successful
someTextField_txt.htmlText = true;
someTextField_txt.htmlText = (newText.info); //info = variable in text file
}
this section is telling FLASH how to react if the textFile WAS LOADED CORRECTLY!
newText.load("someTextFile.txt");
and now finally we actually make the call to LOAD the textFile with our data in it.
k thanks ill try it
hmm it doesnt seem to work :S which parts should i replace? and how should i lay out my text file?
it works... what did YOU try hat makes you say it doesnt work? are you even getting the FAILED TO LOAD error message? are you naming your objects correctly?..giving them INSTANCE names?
the inside of your textFile shold look like as posted above:
make a text field on your stage with the INSTANCE NAME OF:Quote:
&info=your text here....your text here...your text here.....
someTextField_txt
and the NAME of your textFile needs to be:
someTextFile.txt
ok ive just figured out how to do it. Thanks
one last thing though;
how would i get it so i have in the text file:
&tidenburg#14#phonenumber#adress#
and load them all into seperate text boxes
e.g.:
NAME-------AGE-----PHONE NUMBER
tidenburg-----14-------01245354664
tidusen-------14-------012345678
toby----------14-------012456778
well, your WHOLE data (VAR) object is the text file....refered to as: newText
now in the code we have already...we see that the object newText has some "properties"...so fa it only has 1...which is INFO. which is why we call the text like newText.info
you should by now know that all VARS in the text file need to be seperated by the '&' sign..Quote:
someTextField_txt.htmlText = (newText.info); //info = variable in text file
so you would need to seperate them as so: &name=joe&age=14&phone=12345678
and then you AS would be like:
code:
someTextField_txt.htmlText = (newText.name); //info = variable in text file
someTextField_txt2.htmlText = (newText.age); //info = variable in text file
someTextField_txt3.htmlText = (newText.phone); //info = variable in text file
someTextField_txt.htmlText = (newText.info); //info = variable in text file
Is there a way to make the info variable dynamic so I replace the text from a variable call elsewhere?
Im not really following you..
but you could possible do something like:
code:
var textVar = newText.info; //info = variable in text file
someTextField_txt.htmlText = (textVar);
I mean the text file has to have a variable hard coded in it..(at least one)..
I've designed a course that has 2 dynamic text boxes onscreen, each one has an assigned variable that references my text file. I'd like to use a generic variable in the template and rather than renaming each mc I could change the variable dynamically in my actionscript using a counter.
Sort of like info = "page" + counter; (Counter increments/decrements depending on page).
So to recap, I can get your code to work, by replacing variable "info" with page1.
But if I try and declare info = page1; and then run the code I get undefined. Is there a way to assign a value to info???
Thanks for the response btw.
Sorry I still dont understand :(
&info= is in the textFile...
you want to be able to change what text file the textField loads?...or what info the textField reflects from inside the text file?
Here's a sample of my text.txt file:
&title1=Introduction&
&title2=Language Selection&
&title3=Navigation&
&title4=Introduction&
&title5=Table of Contents&
I want something that can load a variable from my text file into a mc. So I understand that - someTextField_txt.htmlText = (newText.title1); will load "Introduction" into my mc.
However I'd like to use a variable to replace title1, so something like:
var textVar = newText.title1;
someTextField_txt.htmlText = (newText.textVar); //info = variable in text file
But when I do this I get undefined.
In my current setup each page has the variable in the dynamic text box, I don't want to do that.
Here's a sample of my text.txt file:
&title1=Introduction&
&title2=Language Selection&
&title3=Navigation&
&title4=Introduction&
&title5=Table of Contents&
I want something that can load a variable from my text file into a mc. So I understand that - someTextField_txt.htmlText = (newText.title1); will load "Introduction" into my mc.
However I'd like to use a variable to replace title1, so something like:
var textVar = newText.title1;
someTextField_txt.htmlText = (newText.textVar); //info = variable in text file
But when I do this I get undefined.
I've almost given this up, Iam beginning to believe I can't replace the pointer to the text file with a variable. I see other people have posted similar to me and no-one has answered their queries.
Part of the trouble is trying to sensibly explain this, I guess I'm failing!
well I believe you are correct.... the explaination is what is killing this thread.
but.....this:
code:
var textVar = newText.title1;
someTextField_txt.htmlText = (newText.textVar); //info = variable in text file
would never work.. as you are defining newText twice.. in my example you would only need to use textVar,...NOT newText.newVar (as newText is already defined in the VAR)
however...(and this might be because I am just not understanding)....but I think you have things messed up..
your textFile should NOT have & at the end of the var value like that..ONLY when seperating each VARQuote:
&title1=Introduction&
&title2=Language Selection&
&title3=Navigation&
&title4=Introduction&
&title5=Table of Contents&
but using your example...once you load that textFile...ALL variables in the textFile are accessable.
in the Flash 8 help panel, is said to use:
varDynamicTextfielName=
instead of
&varDynamicTextfielName=
Is the only reason for using the amersand sign: &, to seperate the variableNames?, or are there situation you must use the & sign being the first character at the first line.
no....you 'shouldnt' need to start with an &..but it shouldnt hurt either..
and yes..you use an "&" to seperate the variables..... that way you can put many different variables in one text file.
sorry for bumping this.....i typed in the wrong one :mrpimp:
if you want to dynamicly change the contents of your dynamic text field all you'll need to do is this.
At some point. say a button press you run the following code:
First you load in your text file and define your variable, in this case it's the first variable from the text file.
Next you code your buttonCode:loadText = new LoadVars();
loadText.load("myText.txt");
var myVariable = loadText.title1;
Then the last step is to tie your dynamic text field to the variable. In the "Var" input box in the Properties panel for you dynamic text field you enter the variable "_root.myVariable". That should do it.Code:on (release){
_root.myVariable = _root.loadText.title2;
}
Quote:
Originally Posted by whispers
I'm missing something here (in reference to Whisper's first response)- where exactly does one put the above information? Inside the textFile...but...um...where is that?
sorry.. the above you quoted should be:
not info.Quote:
&data=your text here....your text here...your text here.....
you need to MAKE a text file. if you want to load text form an EXTERNAL.txt file.. you need to create one for it to load/read.
newText.txt is the name fo the textFiel you need to create.. save in same folder as your .swf
Okay- all I really want to do is for the dynamic text to load my email address as a way to avoid having spambots read it, so I really don't need an external file. How would I do that?
you make it a button.. then it wont be text... where are you talking about doing this??
Okay- that's easy enough. (For my contact page.) Thanks!