A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: load a random xml item every 5 seconds?

  1. #1
    Senior Member
    Join Date
    Aug 2003
    Posts
    101

    load a random xml item every 5 seconds?

    Hi all, Im not too hot with xml and I could do with some help.

    This may be simple for you guys so how do you do this:

    I have an flv playing, at the end of it playing I have a watch again button or random question button appear (same as you tube).

    What I need to do is for the random question text field to change at a specified time, for example every 5 seconds.

    I need the questions to be pulled in from an external file, i.e an xml file.

    So how do I pull in a random item (a text question) from an xml file that changes every 5 seconds?

    Hope this makes sense, many many thanks

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    from an xml file that changes every 5 seconds

    can you clarify -

    are you loading a fresh xml file every 5 seconds and displaying one random question ?
    or are you loading one xml file, and looping randomly through its questions ?

    thanks

  3. #3
    Senior Member
    Join Date
    Aug 2003
    Posts
    101
    Hi,

    i want a list of say 10 questions that reside in an xml file, and a random question is pulled from this xml list and replaces the old question on the stage every 5 seconds.

    thanks, hope thats a bit clearer.

  4. #4
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    PHP Code:
    // dynamic textfield - instance name - my_txt
    // and this code on main timeline

    _xml = new XML();
    _xml.ignoreWhite = true;
    _xml.load("questions.xml");

    arr = [];

    _xml.onLoad = function() {
    var cNodes:XMLNode = this.firstChild.firstChild;
    while(cNodes) {
    arr.push(cNodes.attributes.question); // add questions to array
    cNodes = cNodes.nextSibling;
    }
    randomize();    
    };

    function randomize(){
    tmp = arr.concat(); // copy the array
    tmp.sort(function(){return Math.floor(Math.random()*3)-1}); // random shuffle
    showQuestion(); // initiate
    questionInt = setInterval(showQuestion,5000); // 5000ms = 5 sec interval
    };

    function showQuestion(){
    trace(tmp);
    if(tmp.length){ // if not empty
    my_txt.text = tmp.pop(); // remove and show last element
    } else {
    clearInterval(questionInt);
    randomize(); // start again
    }
    };

    /* questions.xml -

    <?xml version="1.0"?>
    <anode>
    <cnode question="Question 1" />
    <cnode question="Question 2"  />
    <cnode question="Question 3"  />
    <cnode question="Question 4"  />
    <cnode question="Question 5"  />
    <cnode question="Question 6"  />
    </anode> 

    */
    Last edited by a_modified_dog; 06-14-2008 at 07:14 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center