|
-
Difficulties loading and formatting external txt files -- Solved!
I have been a quiet lurker on these forums for the better part of a year now, searching through old posts for the answers to problems that I have encountered. Recently, though, loading and formatting text from external .txt files has caused me so much difficulty that, even given the numerous other questions that are similar to this that have been addressed, I still find myself without answer and, in my frustration, have actually created an account on flashkit at last.
I use Flash MX 2004 and I want to create dynamic text boxes at runtime, populate them with text from an external text file, and then format them to suit the style of my flash page. This sounded to me, sometime last week when the idea occured to me, a perhaps complex idea, but an elegently customizable one and worth the effort.
This, of course, has not proved as easy as I would hope. My issue is this: when I attempt to load a text file saved in the local directory, it does indeed successfully load, but I am inable to reference it after it does. Thus:
code:
_root.skemaVars = new LoadVars();
_root.skemaVars.load("littletxt.txt");
_root.skemaVars.onLoad = function(success) {
if(success) {
tekst = this.tekst;
trace(_root.skemaVars.tekst);
} else {
trace("File not loaded. Try again, dummy!");
} trace(_root.skemaVars.tekst);
}
trace(_root.skemaVars.tekst);
This code is essentially a direct copy of code posted by "gobbles" about a month ago, but does not differ substantially from any one of a dozen posts of its kind that I have read.
The file "littletxt.txt" reads : tekst="How I wish that this would work."
It is apparent that, in attempt to debug this problem of mine, I have added numerous trace statements to try and spot where things break down. When I run the program, I receive the following three statements in the "Output" window:
undefined
"How I wish that this would work."
"How I wish that this would work."
I do not know whether my inability to reference the variable from outside the curly brackets that compose the LoadVars stems from a lack of understanding of dot syntax or if I am doing something else wrong, but the fact is that I am at my wits end.
Sorry abuot the long nature of this post; any help in this matter would be very much appreciated.
Pacem!
-- T
Last edited by designerTrevor; 09-08-2005 at 11:32 AM.
Reason: Problem solved!
-
Senior Member
The undefined comes from the trace statement outside of the onLoad event. The reason it is undefined is because flash reaches that point in the code before the txt file loads and thus there is no _root.skemaVars.tekst.
_t
-
Ah. Thank you. The unresolved problem remains for me, however, that I cannot actually manage to make the dynamic text box with the instance name "tekst " actually load any of the text that I have assigned.
Is there some line of code that assigns this that I have omitted?
Pacem!
--T
-
ondata's easier, and you're need to assign the string coming in from teh file to the text value of the textbox rather than the textbox itself. without seeing what you've got entirely you can probably fix it by appending ".text" to the instance name, like so
Code:
root.skemaVars.onLoad = function() {_root.tekst.text = this.tekst;}
-
Brilliant! It works!
Thanks both very much.
Just because this might well help some other person, here's the working code
code:
var popUpStyle:TextFormat = new TextFormat();
popUpStyle.font = "Arial";
popUpStyle.size = 20;
popUpStyle.align = center;
var fieldSize:Object = popUpStyle.getTextExtent(tekst);
_root.skemaVars = new LoadVars();
_root.skemaVars.load("littletxt.txt", _root);
_root.skemaVars.onLoad = function() {
_root.tekst.text = this.tekst;
tekst.setTextFormat(popUpStyle);
};
this.createTextField("tekst", this.getNextHighestDepth, 100, 100, 300, 30);
Thanks to you both again.
Pacem!
--T
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|