Derfder
Derfder

Reputation: 3324

Random quote for each load from this XML in SWISHmax

I have this stuff in my randommessages.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<messages> 
    <message name="First message is great" url="http://www.google.com/" /> 
    <message name="Second message is better. I hope so." url="http://www.yahoo.com/" /> 
    <message name="Third is the bomb. Dwonload now! Ok!" url="http://www.facebook.com/" /> 
</messages>

ANd this code I am using for showing on stage:

onSelfEvent(load) 
{ 
    messageXml = new XML(); 
    messageXml.ignoreWhite = true; 
    messageXml.load("randommessages.xml"); 
    messageXml.onLoad = function(success) { 
    if (success) { 
        // some code here
        // ???? 
    } 
} 

} 
else { 
// failed loading
} 
}; 
dynamicmessageoutput._visible = false; 
}

Actually, not showing.

I need help how to get one random message and show it on stage in a dynamic text field called dynamicmessageoutput.

Upvotes: 0

Views: 415

Answers (1)

net.uk.sweet
net.uk.sweet

Reputation: 12431

I'm not quite sure what you're trying to do with the onSelfEvent handler, but the following should provide you a good starting point for what you're trying to do:

var messageXml = new XML(); 
messageXml.ignoreWhite = true; 
messageXml.load("randommessages.xml"); 
messageXml.onLoad = function(success) { 
    if (success) { 
        // Get all the message nodes in an Array
        var messages = this.firstChild.childNodes;
        // Choose a random node from the Array
        var randomNode = messages[Math.round(Math.random() * (messages.length - 1))];
        // Grab the value of the 'name' attribute from the randomly selected node
        var randomMessage = randomNode.attributes['name'];

        trace(randomMessage);
    } else {
        trace("loading failed");
    }
}

Upvotes: 1

Related Questions