Posted By

Atomicflare on 01/26/11


Tagged

text


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

russdogg
kure69
carface
merritt212


AS3 Load Txt


 / Published in: ActionScript 3
 

Simple load External text Class input: string for url output: string

  1. package {
  2.  
  3. import flash.net.URLLoader;
  4. import flash.net.URLRequest;
  5. import flash.events.Event;
  6. import flash.events.IOErrorEvent;
  7.  
  8. public class GetExternalText {
  9.  
  10. private var _txtFile:URLLoader;
  11. private var _txtString:String;
  12.  
  13. public function GetExternalText() {
  14.  
  15. }
  16.  
  17. public function loadText(txtFile:String):void {
  18.  
  19. _txtFile = new URLLoader();
  20. _txtFile.load(new URLRequest(txtFile));
  21. _txtFile.addEventListener(Event.COMPLETE, onLoadTXT, false, 0, true);
  22. _txtFile.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);//missing?
  23.  
  24.  
  25. }
  26.  
  27.  
  28. private function onLoadTXT(evt:Event):void {
  29. _txtString = evt.target.data;
  30. //clean up
  31. _txtFile.removeEventListener(Event.COMPLETE, onLoadTXT);
  32. _txtFile.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  33. }
  34.  
  35.  
  36. private function ioErrorHandler(evt:IOErrorEvent):void {
  37. trace("The following file could not be loaded: " + evt.text);
  38. }
  39.  
  40. public function get loadedText():String {
  41. _txtString ||= "none loaded"
  42. return _txtString;
  43. }
  44.  
  45. }
  46.  
  47. }

Report this snippet  

You need to login to post a comment.