View Full Version : read in XML, load first 'set'
chino
06-28-2009, 01:53 AM
So I have a function that pulls in buttons from XML. These buttons once pressed then load in another image and text from the same XML. It all works great but I'd like to have the first button content show when the movie is loaded.
Here is the code for the XML button and content:
function showArticle(event:MouseEvent):void
{
imageLoader = new Loader();
imageLoader.load(new URLRequest(event.target.name));
imageLoader.x = 20;
imageLoader.y = 20;
textf.addChild(imageLoader);
textf.addChild(articleText);
articleText.x = imageLoader.x;
articleText.y = 97;
articleText.height = 600;
articleText.wordWrap = true;
articleText.width = 365;
articleText.selectable = false;
articleText.multiline = true;
for(var j:int = 0; j < xmlList.length(); j++)
{
if(xmlList[j].attribute("source") == event.target.name)
{
articleText.text = xmlList[j];
articleText.setTextFormat(myFormat);
mySb.direction = "vertical";
// Size it to match the text field.
mySb.setSize(articleText.width, articleText.height + 106);
mySb.move(articleText.x, articleText.width + articleText.y);
mySb.x = 400;
mySb.y = 81;
mySb.scrollTarget = articleText;
addChild(mySb);
trace (articleText.height);
if(articleText.textHeight < 600)
{
removeChild(mySb);
}
}
}
}
Any help is greatly appreciated.
wvxvw
06-28-2009, 10:41 AM
Well... I have to confess, I don't really understand what you wanted your code to do...
So far my observation would be that:
imageLoader has no listeners, so, you don't know when is it loaded.
I don't know what xmlList is.
I don't see why do you need that loop and not just take the attribute you need... / why do you first add and then remove the mySb, whatever it is, if you could check articleText.textHeight before adding it.
Or... was it a problem to call this listener upon first XML loading?
then, I guess, you'd need to do something like:
firstButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
Or, MOUSE_DOWN, or whatever other event showArticle is meant to listen to.
chino
06-28-2009, 07:14 PM
Sorry, I didn't post all of the code, just the function.
Here is all of the code:
import flash.net.URLLoader;
import fl.controls.UIScrollBar;
import flash.events.Event;
var mySb:UIScrollBar = new UIScrollBar();
//--------------
var articleText:TextField = new TextField();
var imageLoader:Loader;
var firstLoader:Loader;
var firstImageLoader:Loader;
var xml:XML;
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("xml/series01.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
var myFormat:TextFormat = new TextFormat();
myFormat.font = "Georgia";
myFormat.color = 0x333333;
myFormat.size = 14;
myFormat.bold = false;
function xmlLoaded(event:Event):void
{
xml = XML(event.target.data);
xmlList = xml.children();
for(var i:int = 0; i < xmlList.length(); i++)
{
imageLoader = new Loader();
imageLoader.load(new URLRequest(xmlList[i].attribute("thumb")));
imageLoader.x = i * 110;
imageLoader.y = 0;
imageLoader.name = xmlList[i].attribute("source");
addChild(imageLoader);
trace(imageLoader.name);
imageLoader.addEventListener(MouseEvent.CLICK, showArticle);
}
}
//-------------- function to load first article
//stage.addEventListener(Event.ENTER_FRAME, showFirstArticle);
xmlLoader.addEventListener(Event.COMPLETE, showFirstArticle);
function showFirstArticle(event:Event):void
{
//xmlList = xml.children();
trace(xmlList.length());
articleText.x = 20;
articleText.y = 97;
articleText.height = 600;
articleText.wordWrap = true;
articleText.width = 360;
articleText.selectable = false;
articleText.multiline = true;
for(var k:int = 0; k < xmlList.length(); k++)
{
if(xmlList[k].attribute("source") == "images/article01btn.jpg")
{
firstImageLoader = new Loader();
firstImageLoader.load(new
URLRequest(xmlList[k].attribute("thumb")));
firstImageLoader.x = k * 110;
firstImageLoader.y = 0;
firstImageLoader.name = xmlList[k].attribute("source");
trace(firstImageLoader.parent);
textf.addChild(firstImageLoader);
firstLoader = new Loader();
//firstLoader.load(new URLRequest(xmlList[0]));
firstLoader.x = k * 110;
firstLoader.y = 0;
articleText.text = xmlList[k];
//trace (firstLoader.name);
textf.addChild(articleText);
articleText.setTextFormat(myFormat);
mySb.direction = "vertical";
// Size it to match the text field.
mySb.setSize(articleText.width, articleText.height + 106);
mySb.move(articleText.x, articleText.width + articleText.y);
mySb.x = 400;
mySb.y = 81;
mySb.scrollTarget = articleText;
addChild(mySb);
if(articleText.textHeight < 600)
{
removeChild(mySb);
}
}
}
}
//-------------- buttons to control articles
function showArticle(event:MouseEvent):void
{
imageLoader = new Loader();
imageLoader.load(new URLRequest(event.target.name));
imageLoader.x = 20;
imageLoader.y = 20;
textf.addChild(imageLoader);
textf.addChild(articleText);
articleText.x = imageLoader.x;
articleText.y = 97;
articleText.height = 600;
articleText.wordWrap = true;
articleText.width = 360;
articleText.selectable = false;
articleText.multiline = true;
trace (event.target.name);
for(var j:int = 0; j < xmlList.length(); j++)
{
if(xmlList[j].attribute("source") == event.target.name)
{
articleText.text = xmlList[j];
articleText.setTextFormat(myFormat);
mySb.direction = "vertical";
// Size it to match the text field.
mySb.setSize(articleText.width, articleText.height + 106);
mySb.move(articleText.x, articleText.width + articleText.y);
mySb.x = 400;
mySb.y = 81;
mySb.scrollTarget = articleText;
addChild(mySb);
trace (articleText.height);
if(articleText.textHeight < 600)
{
removeChild(mySb);
}
if(imageLoader.name == "images/article01btn.jpg")
{
trace(imageLoader.name);
}
else
{
textf.removeChild(firstImageLoader);
}
}
}
}
It now pulls in the first node in the XML and when I press another button, the "firstImageLoader" goes away as intended but I'm getting this error.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at series01_fla::MainTimeline/showArticle()
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.