PDA

View Full Version : It's probably easy, but how do I make it random?



Shilakadaddy
May 21st, 2007, 12:58 PM
I've got an XML menu, followed the squirrel tutorial and modified the code to suit what I was doing (a news page). Now I'm wanting to generate a single random news entry from this XML document, but I can't work out how to do it at all!!

I'm guessing the answer is to change something in here, but everything I've tried hasn't worked out so far :(


function CreateMenu(menu_xml) {
// start with the first item in the XML
var items = menu_xml.firstChild.firstChild.childNodes;
for (var i=0; i<items.length; i++) {
// only continue if the type of this item is news
if (items[i].attributes.type == "news") {
// create variables for our elements
var date = items[i].childNodes[0]; // first child node
var newsTitle = items[i].childNodes[1]; // second child node
var moreInfo = items[i].childNodes[2]; // third child node

// Create a menu item movieclip in the menuMC instance on the main timeline
// for each item element offsetting each additional further down the screen
var itemMC = menuMC.attachMovie("menu_item","item"+item_count, item_count);
itemMC._y = item_count * item_spacing;
item_count++;

// assign text using nodeValue to get the text from the text nodes
itemMC.date_txt.text = date.firstChild.nodeValue;
itemMC.newsTitle_txt.text = newsTitle.firstChild.nodeValue;
itemMC.mainBtn.title_txt = newsTitle.firstChild.nodeValue;
itemMC.mainBtn.story_txt = moreInfo.firstChild.nodeValue;
// when itemMC is pressent, calls DisplayInfo function
itemMC.mainBtn.onRelease = DisplayInfo;
}
}
}

Any help would be appreciated :)

Cheers, Chris

Shilakadaddy
May 21st, 2007, 01:15 PM
bish bash bosh


// start with the first item in the XML
var items = menu_xml.firstChild.firstChild.childNodes;
var randomGen:Number = random(5);
for (var i=randomGen; i<randomGen+1; i++) {

all done :)