Skip to main content
Inspiring
September 1, 2009
Answered

Help with XML - AS 3.0

  • September 1, 2009
  • 2 replies
  • 1192 views

Hello everybody. I decided to start working with XML since it allows me to load assets externally and edit my applications later very easily without having to edit the fla file itself. So today I watched and read a few tutorials and now I'm trying to build an Event Scroll, all through XML and AS 3.0 code. Basically what I want is to have several events inside a movieclip which I will incorporate into a scroll pane when I'm done, for now I'll focus on extracting data from the XML file and parsing it with AS 3.0. The layout is simple: a thumbnail image on the left, and beside it on the right there is the title of the event, its date and some info about it and all of these should have a dark grey background while the text is in white. Each two events are 30 pixels apart and there are 5 events for now.

Ok enough talking now let's get to the code:

XML Code:

<?xml version="1.0" encoding="utf-8"?>

<EVENTSXML>

     <EVENT>

          <TITLE> Event 1</TITLE>

         <DATE>12/04/2009</DATE>

        <THUMB>thumb0.jpg</THUMB>

        <INFO> Some Text Goes Here </INFO>

    </EVENT>

     <EVENT>

           <TITLE>Event 2</TITLE>

         <DATE>03/02/2009</DATE>

        <THUMB>thumb1.jpg</THUMB>

        <INFO>Some Text Goes Here</INFO>

     </EVENT>

     <EVENT>

           <TITLE>Event 3</TITLE>

         <DATE>18/11/2008</DATE>

        <THUMB>thumb2.jpg</THUMB>

        <INFO>Some Text Goes Here</INFO>

     </EVENT>

     <EVENT>

           <TITLE>Event 4</TITLE>

         <DATE>10/09/2008</DATE>

        <THUMB>thumb3.jpg</THUMB>

        <INFO>Some Text Goes Here</INFO>

     </EVENT>

     <EVENT>

           <TITLE>Event 5</TITLE>

         <DATE>06/08/2008</DATE>

        <THUMB>thumb4.jpg</THUMB>

        <INFO>Some Text Goes Here</INFO>

     </EVENT>

</EVENTSXML>

Actionscript 3.0 Code:

var myXML:XML;

var req:URLRequest = new URLRequest("events.xml");

var ldr:URLLoader = new URLLoader();

ldr.load(req);

ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void

{

     myXML = new XML(e.target.data);

     buildEvents(myXML.EVENT);

}

var eventScroll:MovieClip = new MovieClip();

var padding:Number = 30;

this.addChild(eventScroll);

eventScroll.x = eventScroll.y = padding;

function buildEvents(evnts:XMLList):void

{

     for(var i:uint = 0; i <evnts.length(); i++) {

          var eventData:MovieClip = new MovieClip();

          

          eventData.y = (20 + padding) * i;

          eventData.itemNum = i;

          eventData.title = evnts.TITLE;

          eventData.date = evnts.DATE;

          eventData.thumb = evnts.THUMB;

          eventData.info = evnts.INFO;

          

          // thumb container

          thisThumb:Sprite = new Sprite();

          var ldr:Loader = new Loader()

          var req:URLRequest = new URLRequest(eventData.thumb);

          ldr.load(req);

          thisThumb.addChild(ldr);

          eventData.addChild(thisThumb);

          eventScroll.addChild(eventData);

     }

}

now I am stuck there... the application is not done yet and when I publish the file I keep getting errors, these are the errors:

1067: Implicit coercion of a value of type flash.display:Sprite to an unrelated type Class.

Source: thisThumb:Sprite = new Sprite();


1188: Illegal assignment to class Sprite.

Source: thisThumb:Sprite = new Sprite();


1120: Access of undefined property thisThumb.

Source: thisThumb.addChild(ldr);


1120: Access of undefined property thisThumb.

Source: eventData.addChild(thisThumb);


1119: Access of possibly undefined property contentLoaderInfo through a reference with static type flash.net:URLLoader.

Source: ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, processXML);

So where did I go wrong and how can I carry on with my code to finish my application?

P.S: I do not want someone to do it for me, so in case someone decided to help me out, please comment and explain your code because this application is for learning porpuses in the first place... Thanks in advance!

This topic has been closed for replies.
Correct answer

oh I've just noticed one little problem, in my info field, the text doesn't break and start a new line, it just keeps going horizontally although I've set it to multiline and gave it a width and height... so what shall I do to fix this problem? here's the code:

var infoF:TextField = new TextField();

          infoF.text = eventData.info;

          infoF.x = 250;

          infoF.y = dateF.y + padding;

          infoF.multiline = true;

          infoF.width = 200;

          infoF.height = 100;

          addChild(infoF);


infoF.wordWrap = true;

2 replies

Ned Murphy
Legend
September 1, 2009

As for the first group of errors, they may all stem from not declaring a thisThumb "var"

Inspiring
September 1, 2009

Yup, got that fixed too now lol. Now there are no errors and the images are showing up on stage! now I just need to load in the text and align them properly to the right of the images!

Ned Murphy
Legend
September 1, 2009

I couldn't tell you what's wrong now.  With those two fixes in the code, and some images of my own named in the xml file, they load up okay for me when I test.

Ned Murphy
Legend
September 1, 2009

I haven't looked into the code that generates all but the last error as yet, but for that last error, there is no contentLoaderInfo property for the URLLoader class... you just want to add an event listener for the event completing (refer to it in the help docs).  Also, you should always define the listener before you initiate the load command

Inspiring
September 1, 2009

Urgh! that's the second time you tell me about this and I keep doing the same mistake. Thanks for the hint, got that fixed now... I'll move to the other errors now.