1 Reply Latest reply on Oct 29, 2007 6:22 PM by Gregory Lafrance

    Dynamic app creation using MXML?

    wlee@unimelb
      I'm working on creating a small quiz application which will be driven by pre-formatted external XML. I have designed various custom question canvases for each of the question types (MCQ, short answer, match-pairs etc.) and I'm wondering if there is some way to implement the main application using MXML instead of ActionScript?

      I've managed to hard-code the order of the questions, knowing which canvases are needed in which order, but I need to be able to detect the question type from the XML (that bit I can manage) and then add the appropriate question canvas component in order in my tabNavigator.

      Essentially, I'm trying to replicate the function of a switch:case clause in MXML. Does anyone know if this is possible? I've started trying to implement it in ActionScript, but I'm a bit of a newbie to AS3, and I keep getting errors I don't understand, like "Access of undefined property" where I'm referring to an object I've just declared and instantiated in the previous line of code. E.g (anywhere I use mainPanel):

      <mx:Script>
      <![CDATA[
      import mx.controls.Text;
      import mx.containers.Canvas;
      import mx.containers.Panel;

      var mainPanel:Panel = new Panel();

      Root.addChild(mainPanel);

      mainPanel.layout = "absolute";
      mainPanel.setStyle("left", 10);
      mainPanel.setStyle("top", 10);
      mainPanel.setStyle("right", 10);
      mainPanel.setStyle("bottom", 10);

      var testPage:Canvas = new Canvas();
      var numQuestions:int = quizQns.childNodes.length;
      var qTypes:Array = new Array(numQuestions);
      var textLine:Array = new Array(numQuestions);
      ]]>
      </mx:Script>
        • 1. Re: Dynamic app creation using MXML?
          Gregory Lafrance Level 6
          You can declare and initialize variables outside functions in your script block, but for most else you need to do it in a function. I would continue learning AS3 as it will give you real flexibility and power.

          <?xml version="1.0"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
          <mx:Script>
          <![CDATA[
          import mx.controls.Text;
          import mx.containers.Canvas;
          import mx.containers.Panel;

          private var mainPanel:Panel;
          private var testPage:Canvas = new Canvas();
          private var numQuestions:int = 10;
          // private var numQuestions:int = quizQns.childNodes.length;
          private var qTypes:Array = new Array(numQuestions);
          private var textLine:Array = new Array(numQuestions);

          public function init():void {
          mainPanel = new Panel();
          addChild(mainPanel);
          mainPanel.layout = "absolute";
          mainPanel.setStyle("left", 10);
          mainPanel.setStyle("top", 10);
          mainPanel.setStyle("right", 10);
          mainPanel.setStyle("bottom", 10);
          }
          ]]>
          </mx:Script>
          </mx:Application>