2 Replies Latest reply on Jul 4, 2007 9:16 AM by Oliver Krylow

    Embed external .txt file

    Oliver Krylow Level 1
      Hello,

      trying to embed a external txt. file into my mxml file, which should be a rather easy task, I run into following problems:

      This is my AS Code :



      import flash.net.URLLoader;
      import flash.net.URLRequest;
      import flash.events.Event;



      var loader:URLLoader = new URLLoader();
      var request:URLRequest = new URLRequest("text.txt");
      loader.load(request);

      // "ggg" is EventHandler which loads external txt. file in TextArea with id = "textBox"
      loader.addEventListener(Event.COMPLETE, ggg);


      function ggg(event:Event):void
      {
      textBox_txt.text = event.target.data;
      }




      Now for "loader,request and ggg" it is giving me error #1120´which means "Access of undefined property _.".
      Since I distinctly remember declaring the variables loader and request and a function ggg, I do not understand why it is giving this error.

      I have tried this exact same code in the Flash CS3 environment and it worket just fine.

      So what is Flex trying to say to me ^^ ?

      Cheers, Oli
        • 1. Re: Embed external .txt file
          ntsiii Level 3
          You cannot do complex work outside of a function due to the way the mxml/as is compiled.

          Declare the variables at instance-level scope if you need that, but put all of the asignments in a function, say initApp(), that you call from the initialize or creationComplete event of the application.

          Tarcy
          • 2. Re: Embed external .txt file
            Oliver Krylow Level 1
            Hey Tarcy !

            Thank you a lot. I needed a while to understand what you were saying, but it is now making sense to me.

            Just in case somebody wants to know, this is how my code looks like now:


            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()" >

            <mx:Script>
            <![CDATA[
            private function initApp():void
            {
            var loader:URLLoader = new URLLoader();
            var request:URLRequest = new URLRequest("text.txt");
            loader.load(request);
            loader.addEventListener(Event.COMPLETE, ggg);
            }

            private function ggg(event:Event):void
            {
            textBox_txt.text = event.target.data;
            }
            ]]>
            </mx:Script>


            <mx:TextArea width="500" height="500" id="textBox_txt" editable="false" wordWrap="true"/>



            </mx:Application>



            It is working just fine!

            Bye, Oliver