3 Replies Latest reply on Jul 7, 2008 5:01 PM by Caly2007

    Loop

    Caly2007
      I'm having a problem playing a loop. The code attaced shows the loading of XML plus the creation of a question array. I load the initial value of 0 to the textArea on the stage. Now I want to go through the question one at a time by use of a button placed on the stage. How do I achive this?
        • 1. Re: Loop
          clbeech Level 3
          well using the methods you've indicated, you haven't exactly 'store' the questions within the array 'list' as the array will only contain a single index housing the xml doc. a more common way to do this is to iterate through the xml within the onload handler and assign each node value to a new index within the array. if each 'question' node also contains the 'answer' you may wish to create an Object that contains both q and a an then store those Objects at each index in the array. in this way you can advance through the array simply by changing/tracking the current index. a bit more like this:

          (ps. I'm not certain of the xml paths in your doc - i would need to see the xml structure to be certain)
          • 2. Re: Loop
            Caly2007 Level 1
            I'm learning Action Script 2.0 and my instructions are as follows (I want you to get the whole picture.

            Flash Questionnaire Progam, Version 1: Technical Specs
            This document outlines the technical specifications for a Flash‐based questionnaire program. This program allows users to take a timed test, after which they are given a summary indicating their score. The program is intended for deployment as a desktop application (.exe).
            Program Flow
            Loading Screen
            When the program is first opened it should display a startup screen while it loads an external XML document containing a list of questions and answers. When the XML document has been loaded and parsed, a “Start” button should be displayed that takes the user to the Instructions screen.
            Instructions Screen
            The instructions screen displays some general instructions to the user about how to use the survey. It should tell them that they will be timed, and how to answer the questions. This screen should contain a button that says “Start the Survey”, which will take the user into the Questionnaire.
            The Questionnaire
            This is the main part of the application. Each question in the XML document should be displayed, one at a time, along with the answers. When a user clicks an answer, their selection should be recorded, and the next question should be displayed.
            A timer should be running during the course of the questionnaire. This should count down, and when it reaches 0 the questionnaire portion of the application should end, and the user should be taken to the Summary screen.
            Summary Screen
            The summary screen should tell the user how many answers they answered correctly, and how many they answered incorrectly. There should also be a button allowing the user to re‐take the questionnaire. Clicking this button should go back to the Instructions screen.
            Technical Overview
            XML Structure
            The XML document containing the questions should be structured as follows:
            <questions>
            <question>
            <text>This is the text of the question.</text>
            <answers>
            <answer correct=”true”>This is the first option</answer>
            <answer>This is the second option</answer>
            <answer>This is the third option</answer>
            </answers>
            </question>
            </questions>
            The <questions> node may contain multiple <question> nodes, and the <answers> node should contain one or more <answer> nodes.
            The XML file should be parsed into Flash using the XPath API. Information about the XPath API has been included with this delivery. A sample file has also been included that demonstrates how to use the XPath API.
            Questionnaire Elements
            The questionnaire should be composed of a dynamic text field for displaying the question, and multiple instances of an “answer” movieclip for displaying the answers. The answer movieclip should contain a dynamic text field for displaying the answers, and a button that allows users to select that answer.

            help using the XPath class
            • 3. Re: Loop
              Caly2007 Level 1
              1
              XPathAPI class
              ActionScript Class Name mx.xpath.XPathAPI
              The XPathAPI class allows you to do simple XPath searches within Macromedia Flash. This
              can be very useful for searching XML packets based on node names and attribute values. In
              other words, you can quickly find nodes and attributes in an XML document using the
              XpathAPI methods.
              To do XPath searches within Flash, you must first include the XPathAPI class in your Flash
              library by adding the DataBindingClasses component(if it hasn't been added already). If
              you’ve already set up bindings, this class may have been included automatically; otherwise,
              you must select the class from the common libraries (Window > Common Libraries >
              Classes). From the Classes.fla library panel, you can drag a copy of the DataBindingClasses
              component into your current Flash document’s library. You then import the XPathAPI class
              by typing import mx.xpath.XPathAPI, or by using the class’s fully qualified class name as a
              prefix when accessing the class methods (mx.xpath.XPathAPI.method_name).
              Flash supports the following path expressions:
              Type Example
              Absolute path /item/title
              Relative path title if the context node is <item>
              wildcard (*) /*/title retrieves all <title> elements,
              whatever the parent node is
              predicate expressions using =, AND, or OR /item/title[@version='current']
              or:
              /item/title[@version='current' AND
              @post='today']
              2 XPathAPI class
              Method summary for the XPathAPI class
              The following table lists the available methods of the XPathAPI class.
              XPathAPI.getEvalString()
              Availability
              Flash Player 6 (6.0.79.0).
              Edition
              Flash MX 2004.
              Usage
              XPathAPI.getEvalString(node, path)
              Parameters
              node An XML node; a reference to the DOM node that is the parent node from which the
              statement executes.
              path A string identifying the path through the XML node hierarchy.
              Returns
              A string that represents the code required to access the value specified by the path parameter
              from the parent node.
              Description
              Method; for a specified node, gets the corresponding evaluation string.
              Method Description
              XPathAPI.getEvalString() For a specified node, gets the corresponding
              evaluation string.
              XPathAPI.selectNodeList() Retrieves all the node values of a specified node
              level.
              XPathAPI.selectSingleNode() Retrieves the node value of the first node at a
              specified node level.
              XPathAPI.setNodeValue() Sets the value of a specified node or attribute.
              XPathAPI.selectNodeList() 3
              Example
              The following example displays the corresponding evaluation string used for a specified node
              in the Output panel:
              import mx.xpath.XPathAPI;
              var rssfeed_xml:XML = new XML();
              rssfeed_xml.ignoreWhite = true;
              rssfeed_xml.onLoad = function(success:Boolean) {
              trace("onload...");
              if (success) {
              trace("success...");
              // Retrieve all title notes within /rss/channel/item.
              var thePath_str:String = "/rss/channel/item";
              // Get the evaluation string used for the search.
              var myEvalString_str:String = XPathAPI.getEvalString(this.firstChild,
              thePath_str);
              trace("getEvalString: " + myEvalString_str);
              } else {
              trace("error loading XML");
              }
              };
              rssfeed_xml.load(" http://www.flash-mx.com/news/index.xml");
              XPathAPI.selectNodeList()
              Availability
              Flash Player 6 (6.0.79.0).
              Edition
              Flash MX 2004.
              Usage
              XPathAPI.selectNodeList(node, path)
              Parameters
              node An XML node; a reference to the DOM node that is the parent node from which the
              statement executes.
              path A string identifying the path through the XML node hierarchy.
              Returns
              An array of nodes matching the given XPathAPI statement.
              Description
              Method; retrieves all the node values of a specified node level.
              4 XPathAPI class
              Example
              The following example stores all the title node values into an array, and then displays the list
              in the Output panel:
              import mx.xpath.XPathAPI;
              var rssfeed_xml:XML = new XML();
              rssfeed_xml.ignoreWhite = true;
              rssfeed_xml.onLoad = function(success:Boolean) {
              trace("onload...");
              if (success) {
              trace("success...");
              // Retrieve all title notes within /rdf:RDF/item/title.
              var thePath_str:String = "/rdf:RDF/item/title";
              var title_array:Array = XPathAPI.selectNodeList(this.firstChild,
              thePath_str);
              for (var i:Number = 0; i < title_array.length; i++) {
              trace(title_array .firstChild.nodeValue);
              }
              } else {
              trace("error loading XML");
              }
              };
              rssfeed_xml.load(" http://weblogs.macromedia.com/dev_center/index.rdf");
              XPathAPI.selectSingleNode()
              Availability
              Flash Player 6 (6.0.79.0).
              Edition
              Flash MX 2004.
              Usage
              XPathAPI.selectSingleNode(node, path)
              Parameters
              node An XML node; a reference to the DOM node that is the parent node from which the
              statement executes.
              path A string identifying the path through the XML node hierarchy.
              Returns
              The XML node found for the specified path and node, or null if not found.
              XPathAPI.setNodeValue() 5
              Description
              Method; returns a single node from the given node and XPath parameters. If the path
              specified will result in multiple nodes returned, this method will return only the first one
              found.
              Example
              The following example finds the first title node value within the item node, and then displays
              the value in the Output panel:
              import mx.xpath.XPathAPI;
              var rssfeed_xml:XML = new XML();
              rssfeed_xml.ignoreWhite = true;
              rssfeed_xml.onLoad = function(success:Boolean) {
              trace("onload...");
              if (success) {
              trace("success...");
              // Retrieve first title node within /rdf:RDF/item/title.
              var thePath_str:String = "/rdf:RDF/item/title";
              var titleNode_str:String = XPathAPI.selectSingleNode(this.firstChild,
              thePath_str);
              trace(titleNode_str.toString());
              } else {
              trace("error loading XML");
              }
              };
              rssfeed_xml.load(" http://weblogs.macromedia.com/dev_center/index.rdf");
              XPathAPI.setNodeValue()
              Availability
              Flash Player 6 (6.0.79.0).
              Edition
              Flash MX 2004.
              Usage
              XPathAPI.setNodeValue(node, path, newValue)
              Parameters
              node An XML node; a reference to the DOM node that is the parent node from which the
              statement executes.
              path A string identifying the path through the XML node hierarchy.
              newValue A string containing the new value for the node or attribute.
              6 XPathAPI class
              Returns
              The number of nodes updated.
              Description
              Method; sets the value of the node or attribute identified in the path string to the value of the
              newValue parameter.
              Example
              The following example changes the node values for the specified node path, and then displays
              a summary of the changes in the Output panel:
              import mx.xpath.XPathAPI;
              var rssfeed_xml:XML = new XML();
              rssfeed_xml.ignoreWhite = true;
              rssfeed_xml.onLoad = function(success:Boolean) {
              trace("onload...");
              //trace(rssfeed_xml);
              if (success) {
              var i:Number;
              // Retrieve all title notes within /rss/channel/item.
              var thePath_str:String = "/rss/channel/item/dc:creator";
              var creator_array:Array = XPathAPI.selectNodeList(this.firstChild,
              thePath_str);
              trace("before:");
              for (i = 0; i < creator_array.length; i++) {
              trace("\t" + i + ". " + creator_array.firstChild.nodeValue);
              }
              var updatedNodes_num:Number = XPathAPI.setNodeValue(this.firstChild,
              thePath_str, "dave");
              trace(" ***** " + updatedNodes_num + " nodes updated ***** ");
              var creator_array:Array = XPathAPI.selectNodeList(this.firstChild,
              thePath_str);
              trace("after:");
              for (i = 0; i < creator_array.length; i++) {
              trace("\t" + i + ". " + creator_array.firstChild.nodeValue);
              }
              } else {
              trace("error loading XML");
              }
              };
              rssfeed_xml.load(" http://www.flash-mx.com/news/index.xml");