PDA

View Full Version : Switching external HTML files in a dynamic text-field? (AS3)



Markus_D
February 15th, 2008, 11:14 AM
I have a very basic flash document, with a dynamic text field called myText, and a script to load an external html-file into that field. The script is from a tutorial somewhere, so I'm not 100% sure it's optimal, but it works. What I want to do is modify this code so that a clicking a button in this document will switch the input file from demo.html to another HTML-file, say demo2.html.


var url:String = "demo.html";
var loadit:URLLoader = new URLLoader();

loadit.addEventListener(Event.COMPLETE, completeHandler);
loadit.load(new URLRequest(url));

function completeHandler(event:Event):void {
myText.htmlText = event.target.data as String;
}


I'm very much a beginner, just getting to grips with the language, and programming in general, so the Adobe Developer support is way beyond my comprehension.

I think I understand the first five lines and what they do, but the completeHandler-stuff is kinda beyond me. Which I assume is why I'm not getting it to work.

I've added a button (instance name "btn"), and attempted to use a MouseEvent listener to call a function I named "otherText", and that works fine, the listener checks out, but I don't know how to write the function to actually redefine the input. I tried just copying and pasting the code into the function and renaming the variables, but that was doomed to fail. Here's an example of that botched attempt:


var url:String = "demo.html";
var url2:String = "demo2.html";
var loadit:URLLoader = new URLLoader();

loadit.addEventListener(Event.COMPLETE, completeHandler);
loadit.load(new URLRequest(url));

function completeHandler(event:Event):void {
myText.htmlText = event.target.data as String;
}

btn1.addEventListener(MouseEvent.CLICK, otherText);

function otherText(evt:MouseEvent):void {
loadit.addEventListener(Event.COMPLETE, completeHandler);
loadit.load(new URLRequest(url2));

function completeHandler(event:Event):void {
myText.htmlText = event.target.data as String;
}
}

So as you can see, I'm screwed. Any links to understandable tutorials would be appreciated. I've googled my *** off and can't come up with anything that's relevant to AS3 and in my league of comprehension.

excogitator
February 16th, 2008, 07:57 AM
Hi there, I tried out your code without any changes and even used two html files. Everything worked out fine as it is. Both the html files get loaded. Instead of checking any error in the code, try checking the html files. There might be some misplaced code because of which it is not working.

Markus_D
February 16th, 2008, 04:09 PM
Hi there, I tried out your code without any changes and even used two html files. Everything worked out fine as it is. Both the html files get loaded. Instead of checking any error in the code, try checking the html files. There might be some misplaced code because of which it is not working.

Thanks a lot, I switched the HTML files/resaved them and it worked fine. Guess I wasn't quite as off as I though :p

I did however run into another problem when adding more buttons. When I had two buttons, using the exact same code, cut and pasted with changed variables/button names I had issues with the scrollbar. See, one HTML-file is long enough to demand a scrollbar, the other one is not. So when switching to the short text the scrollbar disappears. I guess that's fine, but the problem is that when you switch the input back to the original (long) text, the scrollbar is still gone. I tried using just long texts and that worked fine, but I don't really want to do that for the finished project.

Any ideas on how to track down the error?

wisedoggs
April 13th, 2011, 10:04 PM
I am no expert myself, but if you give the scrollbar an instance name, you can use actionscript to turn it of and on. Within the functions for each button, you could put
scrollbar1.visible=true; if scrollbar1 is the instance name of the scollbar. So let's say you have a dynamic text field named "content1", and two buttons "Help" and "About" to fill content1 with text about one or the other. within the funtions of each button triggered by the click event handler, you could write
scrollbar1.visible=true; for the long one, and
scrollbar1.visible=false; for the short one. If you wanted an indepth funtion to handle it, you could do something that checks the content1.text.length against whether it is greater or less than a certain height, and if so, within the function, make it visible. This is a rough example, but not probably a working one... just to get you on right track..

textLoader.onLoad(eventHandler, tload);
funtion tload() {
if (abouttext.text.length >= content1.height) {
scrollbar1.visible=true;
} else {
scrollbar1.visible=false;
}
}

wisedoggs
April 13th, 2011, 10:08 PM
I am no expert myself, but if you give the scrollbar an instance name, you can use actionscript to turn it of and on. Within the functions for each button, you could put
scrollbar1.visible=true; if scrollbar1 is the instance name of the scollbar. So let's say you have a dynamic text field named "content1", and two buttons "Help" and "About" to fill content1 with text about one or the other. within the funtions of each button triggered by the click event handler, you could write
scrollbar1.visible=true; for the long one, and
scrollbar1.visible=false; for the short one. If you wanted an indepth funtion to handle it, you could do something that checks the content1.text.length against whether it is greater or less than a certain height, and if so, within the function, make it visible. This is a rough example, but not probably a working one... just to get you on right track..

textLoader.onLoad(eventHandler, tload);
funtion tload() {
if (abouttext.text.length >= content1.height) {
scrollbar1.visible=true;
} else {
scrollbar1.visible=false;
}
}


Flash probably can't compare a string length to a pixel height, so you would have to do someting like
if (abouttext.text.length >= sizetext.text.length), where sizetext is jus enough lines of text to not need a scrollbar. So you evaluate whether it is slightly longer than the exact length of text that fits in teh textfield, and if so, add the scrollbar.