3 Replies Latest reply on May 9, 2015 12:38 PM by Ned Murphy

    Passing a string-defined variable to URLRequest

    geoffreym74700334

      Hi folks,

       

      So I've been trying to write a script that will load a file, from which variables will later be extracted from. I've seen many questions related, and some are probably identical. I'm new to this so I didn't really understand the solutions that were given. I've been working on this problem for quite some time now and there has very little progress. Assistance would be tremendously appreciated. Thank you in advance. 

       

      With all of that being said, below is my annotated code, the name of the frames and a comment corresponding to the main line of code that is troubled are boldfaced and underlined.

      #########################################################################################

      // WelcomeBack; This is the first frame.

      // The purpose of which is to simply display "Welcome Back" with a start button.

      // Once the start button is pressed, "Please enter your subject number" is displayed.

      // The user then enters in the number which will be used to load their (already stored) data.

      // This frame seems to work fine.

       

      import flash.events.TimerEvent;

      import flash.utils.Timer;

       

      stop();

      var questions:Array = ["Please enter your subject number."];

      var q:int= 0;

      var gameTimer:Timer = new Timer(240000,1);

      var endTimer:Timer = new Timer(2000,1);

      // var subjectStr:String = new String;

      var fileStr:String = new String;

      var file:Array = new Array;

       

      txtAnswer.restrict="0-9";

      txtAnswer.visible=false;

      txtQuestion.autoSize=TextFieldAutoSize.CENTER ;

       

      btnStartDistractor.addEventListener(MouseEvent.CLICK, nextProblem);

      gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE,endGame);

       

      var ext:Array = ["_Overall.txt"];

      var subject:Array = new Array;

      var yokedSchedule:Array = new Array;

       

      function processInput (evt:KeyboardEvent):void

      { if (evt.keyCode == Keyboard.ENTER && txtAnswer.text != "")

              { q++

                      var subjectStr = txtAnswer.text

                      var nested_array:Array = new Array([subjectStr],[ext]);

                      var fileStr:String = nested_array.join("");  // If the user puts in the number 746, this should assign "746_Overall.txt" to the variable 'fileStr'

                      var file:Array = fileStr.split()   // This is probably unnecessary

                     

                      {gotoAndStop("ReadData")}

                     

                      }

              }

       

       

             

      function nextProblem(evt:Event):void

      { if (q<questions.length)

              {

                      txtAnswer.text="";

                      txtQuestion.text=questions[q];

                      stage.addEventListener(KeyboardEvent.KEY_DOWN, processInput);  

               if (q==0)

                      {

                              txtAnswer.visible=true;

                              btnStartDistractor.removeEventListener(MouseEvent.CLICK, nextProblem);

                              btnStartDistractor.visible=false;

                              gameTimer.start();     

                      }

              }

              else if (q==questions.length)

              {       txtQuestion.text="";

                      txtAnswer.text="";

                              gameTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,endGame);

                              stage.removeEventListener(KeyboardEvent.KEY_DOWN, processInput);

                          {gotoAndStop("ReadData")}

                      }

              }

       

      trace(txtAnswer.text)

      function endGame(evt:TimerEvent):void{

              endTimer.addEventListener(TimerEvent.TIMER_COMPLETE,endGame)

              endTimer.start()

              txtQuestion.text="TIME'S UP!";

              txtAnswer.text="";

      }

       

      #########################################################################################

      // ReadData; This is the second frame.

      // The purpose of this frame is to load the file (e.g. 746_Overall.txt), using the variable called fileStr

      // As of now, it can load a string-defined variable (i.e. url), however, it doesn't seem to work when url is replaced with fileStr

       

      import flash.net.URLRequest;

      import flash.net.URLLoader;

      import flash.net.URLVariables;

      import flash.net.URLRequestMethod;

      import flash.events.Event;

       

       

      stop();

       

       

      // the path to the backend file

      var url : String = '746_Overall.txt'; // This variable works;

      trace(fileStr)

      var urlVariables : URLVariables = new URLVariables ();

               trace(urlVariables)

       

      var request : URLRequest = new URLRequest  ( url ); // This works, however, when replaced with fileStr it does not work.

          request.method = URLRequestMethod.GET;

       

      var loader : URLLoader = new URLLoader ();

         // loader.addEventListener( Event.COMPLETE, handleLoaderComplete )

          loader.load ( request );

              loader.addEventListener(Event.COMPLETE, onLoaded);

             

       

       

              function onLoaded(e:Event):void

      {

              yokedSchedule = e.target.data.split(","); //split patterns up using the comma as a delimiter, otherwise the entire schedule would be one element

              scheduleTextBox.text = String(yokedSchedule);

              // trace(yokedSchedule)

      }