PDA

View Full Version : Create Actionscript for new movie?


jontucker
02-22-2006, 09:34 AM
In Actionscript (Fmx) it possible to create a new movie with createEmptyMovieClip, and then attach some dynamically created actionscript to it?

Cota
02-22-2006, 04:59 PM
To a degree you can. Using movieclip.onEnterFrame then attaching your code.

jontucker
02-22-2006, 06:16 PM
Ok, what I actually want to do is create a load of movies from each record in an XML file, then create a text field in each of the movies which displays the record.

Then, I want to be able to use onPress for each of these movies (of which there could be any number) to perform a function, specifically to go to another frame and display another record from the XML.

As you may have guessed I'm trying to put together a news page, and struggling!

Cota
02-22-2006, 08:03 PM
What code do you currently have?

jontucker
02-23-2006, 10:03 AM
Hi, heres my code that imports the XML file and creates a movie for each record, then creates a text field for the news title and date and sets the .text value to the relevant record....



news_xml = new XML();
news_xml.ignoreWhite = true;
news_xml.onLoad = function(sucess) {
if (sucess) {
processNews(news_xml);
}
};
news_xml.load('textfiles/news.xml');




function processNews(xmlDoc_xml) {

for (var n = 0; n<xmlDoc_xml.firstChild.childNodes.length; n++) {
news_mc.createEmptyMovieClip("news_mc_" + n, n);
news_mc["news_mc_" + n]._y = -20 + (20*n);
news_mc["news_mc_" + n].createTextField("news_title_" + n, n, 0, 0, 400, 100);
news_mc["news_mc_" + n].createTextField("news_date_" + n, n+1, 400, 0, 200, 100);
news_mc["news_mc_" + n]["news_title_" + n].text = xmlDoc_xml.firstChild.childNodes[n].attributes.title;
news_mc["news_mc_" + n]["news_date_" + n].text = xmlDoc_xml.firstChild.childNodes[n].attributes.date;
}


}


What I want to do is set onPress for each new movie created and have that run a function that goes to another frame to display the news item that was selected.

Seems like it should be simply but I cant quite get my head around it. Any info would be greatly appreciated!

jontucker
02-23-2006, 11:21 AM
I think I've sorted it now...


news_xml = new XML();
news_xml.ignoreWhite = true;
news_xml.onLoad = function(sucess) {
if (sucess) {
processNews(news_xml);
}
};
news_xml.load('textfiles/news.xml');

ntf = new TextFormat();
ntf.font = "Arial";
ntf.bold = true;


function processNews(xmlDoc_xml) {

for (var n = 0; n<xmlDoc_xml.firstChild.childNodes.length; n++) {
news_mc.createEmptyMovieClip("news_mc_" + n, n);
news_mc["news_mc_" + n]._y = (20*n);

news_mc["news_mc_" + n].itemnum = n;

news_mc["news_mc_" + n].onPress = function() {
trace (this.itemnum);
}

news_mc["news_mc_" + n].createTextField("news_title_" + n, n, 0, 0, 350, 20);
news_mc["news_mc_" + n].createTextField("news_date_" + n, n+1, 350, 0, 100, 20);
news_mc["news_mc_" + n]["news_title_" + n].text = xmlDoc_xml.firstChild.childNodes[n].attributes.title;
news_mc["news_mc_" + n]["news_date_" + n].text = xmlDoc_xml.firstChild.childNodes[n].attributes.date;
news_mc["news_mc_" + n]["news_title_" + n].setTextFormat(ntf);
news_mc["news_mc_" + n]["news_date_" + n].setTextFormat(ntf);
trace (xmlDoc_xml.firstChild.childNodes.length);
}


}

This does produce the correct results in the output window so now all I have to do is pass 'this.itemnumber' on to another frame!

Thanks for pointing me in the right direction Cota!

jontucker
02-23-2006, 12:03 PM
The last thing I need to do, which again is thwarting me, is putting news_mc in a scrollpane. Can you help Cota?

Cota
02-23-2006, 02:18 PM
I would have attached the scrollpane, through code, before the for loop, then created the empty movieclips inside the scrollpane.

jontucker
02-23-2006, 03:25 PM
Grrrr, I've got it scrolling now but the scrollpane isnt masking. I'm going round in circles with this. I've attached it, if you have a sec could you have a look at tell me what I'm doing wrong? Thanks again for your help so far Cota.

jontucker
02-23-2006, 06:12 PM
Right, I've sorted it. Dont know why but it seems that if I attach a movie to the stage with text fields already in it, instead of creating a new movie and new text fields for each record...it works!!

Cota
02-23-2006, 09:00 PM
I apologize for taking so long to get back to you, I'm glad you have it sorted.

jontucker
02-23-2006, 09:45 PM
No problem Cota, really appreciate your help. Here (http://www.stuartjonesrallying.com/sjm) is what I've been working on!

Cota
02-23-2006, 11:16 PM
Looking good so far..