A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Parse Text and Images In XML Question

  1. #1
    Member
    Join Date
    Sep 2010
    Posts
    61

    Parse Text and Images In XML Question

    I'm still trying to wrap my head around XML and am having trouble getting images and text from the XML file into my Flash document.

    Here is an example of my XML:

    Actionscript Code:
    <?xml version="1.0" encoding="utf-8"?>
     <images>  
        <image src="images/properties/published/300_March_to_Glory_thumbnail.jpg" title ="300 March to Glory" /> </images>

    And here is the code I'm using in my Flash file (it's long, I'll bold what I think is relevant but I'll still post everything in case it helps):

    Actionscript Code:
    //LOAD XML
                function LoadXML(e:Event):void
                {  
                    xmlData = new XML(e.target.data);  
                      buildScroller(xmlData.image);
                 }
               
                [B]//BUILD SCROLLER FROM XML  
                 function buildScroller(imageList:XMLList):void
                {  
                       var scroller:MovieClip = new MovieClip();  
                    this.addChild(scroller);  
                    scroller.y = 85;
                    scroller.x = 15;
                    scroller.mask = scrollerMask;
                       
                     for (var item:uint = 0; item < imageList.length(); item++ )  
                        {  
                        var gameThumb:MovieClip = new MovieClip();
                                             
                        gameThumb.y = (130 + padding) * item;  
                        gameThumb.itemNum = item;  
                         gameThumb.src = imageList[item].attribute("src");
                        gameThumb.title = imageList[item].attribute("title");
                                                           
                        //CREATE IMAGE CONTAINER  
                        var thisThumb:Sprite = new Sprite();
                       
                        //PARSE IMAGES  
                        var ldr:Loader = new Loader();  
                        var urlReq:URLRequest = new URLRequest(gameThumb.src);  
                        ldr.load(urlReq);  
                        thisThumb.addChild(ldr);  
                        gameThumb.addChild(thisThumb);
                                           
                        gameThumb.alpha = 0;[/B]
                         
                        //ADD SCROLLER TO STAGE  
                        scroller.addChild(gameThumb);  
                }

    I want the text in "Title" to show up next to the image in the list. The actual XML file has 70 items, but I didn't want to burden you with them since they are all the same format.

    Can anyone help me figure this out? Am I being clear? If you need more information, please ask.

    Thank you,

    Ogaac

    EDIT: I should mention that the images work fine, I just can't get the text to show up.
    Last edited by ogaac; 10-26-2010 at 02:00 PM. Reason: Deleted (hopefully) irrelevant code for ease of reading...

  2. #2
    Member
    Join Date
    Sep 2010
    Posts
    61
    Ok I think I'm on the right track, but I'm getting an error message I don't know how to fix. Here's my code:

    Actionscript Code:
    for (var item:uint = 0; item < imageList.length(); item++ )  
                        {  
                        var gameThumb:MovieClip = new MovieClip();
                        var gameTitle:TextField = new TextField();
                                             
                        gameThumb.y = (130 + padding) * item;  
                        gameThumb.itemNum = item;  
                         gameThumb.src = imageList[item].attribute("src");
                       
                        gameTitle.y = (130 + padding) * item;
                        gameTitle.x = 50 * item;
                        //gameTitle.itemNum = item;
                        gameTitle.text = imageList[item].attribute("title");
                                                           
                        //CREATE IMAGE CONTAINER  
                        var thisThumb:Sprite = new Sprite();
                        var thisText:TextField = new TextField();
                       
                        //PARSE IMAGES  
                        var ldr:Loader = new Loader();  
                        var urlReq:URLRequest = new URLRequest(gameThumb.src);  
                        ldr.load(urlReq);  
                        thisThumb.addChild(ldr);  
                        gameThumb.addChild(thisThumb);
                        gameTitle.addChild(thisText);
                                                               
                        gameThumb.alpha = 0;

    And here's the error message:

    Actionscript Code:
    1061: Call to a possibly undefined method addChild through a reference with static type flash.text:TextField.

    How is addChild undefined? Can I not use it for a TextField? What would I use instead?

    Thanks,

    Ogaac

  3. #3
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    gameTitle.addChild(thisText); is the problem. You're trying to add a textField to a textField.
    .

  4. #4
    Member
    Join Date
    Sep 2010
    Posts
    61
    But I get the same message when I try to add it to thisThumb.

    Here's my new code:

    Actionscript Code:
    for (var item:uint = 0; item < imageList.length(); item++ )  
                        {  
                        var gameThumb:MovieClip = new MovieClip();
                        var gameTitle:TextField = new TextField();
                                             
                        gameThumb.y = (130 + padding) * item;  
                        gameThumb.itemNum = item;  
                         gameThumb.src = imageList[item].attribute("src");
                       
                        gameTitle.y = (130 + padding) * item;
                        gameTitle.x = 50 * item;
                        //gameTitle.itemNum = item;
                        gameTitle.text = imageList[item].attribute("title");
                                                           
                        //CREATE IMAGE CONTAINER  
                        var thisThumb:Sprite = new Sprite();
                                           
                        //PARSE IMAGES  
                        var ldr:Loader = new Loader();  
                        var urlReq:URLRequest = new URLRequest(gameThumb.src);
                        var urlReqT:URLRequest = new URLRequest(gameTitle.text);
                        ldr.load(urlReq);  
                        thisThumb.addChild(ldr);  
                        gameThumb.addChild(thisThumb);
                        gameTitle.addChild(thisThumb);
                                                               
                        gameThumb.alpha = 0;

    And the error message:

    Actionscript Code:
    1061: Call to a possibly undefined method addChild through a reference with static type flash.text:TextField.

    I'm sure I'm making a simple mistake, I just can't seem to spot it :/

    EDIT: I think I was doing it wrong, I switched gameTitle.addChild(thisThumb) to gameThumb.addChild(gameTitle); which now throws no error, but I don't see any text...
    Last edited by ogaac; 10-27-2010 at 11:38 AM.

  5. #5
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Could I see the fla please
    .

  6. #6
    Member
    Join Date
    Sep 2010
    Posts
    61
    I took out a bunch of pictures you wouldn't need to make the file size smaller, thanks for taking a look.

    http://kyleaho.com/ManagementTest.zip

  7. #7
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    I have CS4 so you'll have to upload an older version. What you can do is put a trace to double check that it's getting text to begin with. Also, use gameTitle.border = true; to make sure it's there.
    .

  8. #8
    Member
    Join Date
    Sep 2010
    Posts
    61
    Sorry, I should have specified that before hand. Here's a CS4 version.

    Interestingly enough, every 4th or 5th image in the file I'm giving you has text, still trying to figure out why. I'll keep you posted if I figure anything out...
    Thanks again!

  9. #9
    Member
    Join Date
    Sep 2010
    Posts
    61
    Ok so my Y value on the text fields threw off the whole thing. I think I'm going in the right direction now, embarrassed as I am not noticing it before.

    If you have any comments I'd still love to hear them, otherwise I'll just keep plugging along!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center