The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: XML use outside of EventListener Function?!?!?!?!?!

  1. #1

    Afrostyle XML use outside of EventListener Function?!?!?!?!?!

    Help.. i've been at this for days, how do I get xmlData to load information outside of the function LoadXML? I'm willing to donate to the person who can get me something that works or this to work. I desperately need this.

    Code:
    var xmlData:XML;
    var loade:URLLoader;
    
    loade=new URLLoader(new URLRequest("playlist.xml"));
    loade.addEventListener(Event.COMPLETE,LoadXML);
    
    
    function LoadXML(e:Event):void {
    
        xmlData = new XML(e.target.data);
        trace(xmlData);
    
    }
    
    trace(xmlData) //Outputs undefined?!?!
    Also, I wanted to mention that I have checked this:
    http://www.kirupa.com/forum/showpost...78&postcount=6

    So as a side note I was wondering, if I had to; could I just execute all my actionscript inside of that eventlistener function?

    Thanks
    Last edited by epitomatic; February 17th, 2009 at 12:41 AM.

  2. #2
    It's undefined because when that trace is called, it hasn't been loaded yet.

    You're setting the event listener to wait until the XML file is loaded, then creating an XML object with the data that was loaded. You're declaring the function for the event to call, but then you immediately trace the xmlData that hasn't loaded yet.

    Code:
    var xmlData:XML;
    var loade:URLLoader;
    
    loade=new URLLoader(new URLRequest("playlist.xml"));
    loade.addEventListener(Event.COMPLETE, LoadXML);
    
    var framesUntilLoad:int = 0;
    
    addEventListener(Event.ENTER_FRAME, mainLoop);
    
    function LoadXML(e:Event):void
    {
        xmlData = new XML(e.target.data);
    }
    
    function mainLoop(e:Event):void
    {
        framesUntilLoad++;
        
        if(xmlData != null)
        {
            trace("Frames passed before xmlData was loaded: ", framesUntilLoad);
            trace(xmlData);
            
            removeEventListener(Event.ENTER_FRAME, mainLoop);
        }
    }

  3. #3
    First off, thanks for your help. I have a question though, where do I run the rest of my code, say if I had more action listeners for when someone clicks a button, etc etc?

    Thanks

  4. #4
    Well, if those buttons rely on the XML data being loaded, you could just check if the data is loaded (like the "!= null" part of my example) and not run the button actions if it isn't.

    In this case it's more of a design problem than a coding one; for example, you could have the buttons grayed out until the XML data is loaded, or you could hide them and display a "loading..." type thing until the XML data is loaded then show the buttons.

    Can you explain what you're going to be doing with the buttons?

  5. #5
    Ahh, so maybe a pre-loader then proceed on once finished?

    I'm working on a basic music player pretty much; the code isn't on what is posted but i've got the buttons down and everything. I'm just having problem with the very thing I posted, loading an xml "playlist" file that can be read etc etc.

    This is also really my first app attempt in actionscript but i'm fairly well versed in JAVA and PHP so I've caught most other things pretty easily. I'm pretty stuck on this though.

    Thanks

  6. #6
    I've put together an example for you.

    In the document class, MainAS.as, it displays a "Loading..." animation until the XML data is loaded, then it calls the createPlaylist method which then removes the loading animation and passes the XML data to a new instance of Playlist.

    The Playlist class takes the XML data and loops through it, creating instances of the PlaylistItem class, giving it the necessary information it needs to set itself up (title and artist strings in this simple example).

    Let me know if you need help understanding the example.
    Attached Files Attached Files

  7. #7
    Hey, thanks for the reply; I indeed have a question about the file. Looks great but is there any way I could possibly just place that into one class and run it from the client? Also how is the client pulling the information/instantiating the class, etc because I couldn't find any code in Main.As

    Thanks

  8. #8
    Ahh, nevermind, I had Main.FLA opened lol; so I assume after this in MainAS:

    Code:
                new URLLoader(new URLRequest("playlist.xml")).addEventListener(Event.COMPLETE, createPlaylist);
                addChild(loadingText);
    I could just manipulate the information that's now in Playlist as I choose? Also, would it by chance be better inside of Playlist to do an array with PlaylistItems so that I can manipulate them (functions like I suppose, nextTrack, prevTrack, etc etc)? I'm unsure but definitely would love your feedback on this,

    Thanks

  9. #9
    If you want to make working with the playlist data easier, you could convert the XML into an array by looping through it and pushing tracks into a new array. You could work with the XML just the same though; for exmaple, you can change the loop code in Playlist to this and it will do the same thing:

    Code:
    for(var i:int = 0; i < n; i++)
    {
    	var track:XML = playlistXML.track[i];
    	
    	var playlistItem:PlaylistItem = new PlaylistItem(track.title, track.artist);
    	    playlistItem.y = playlistHeight;
    	
    	playlistHeight += playlistItem.height;
    	
    	addChild(playlistItem);
    }
    So you can just have an int currentTrack or something and when you want to play the next track, just increase it.

    Code:
    var track:XML = playlistXML.track[trackNumber];

  10. #10
    Appreciate it, however I may as well ask because i'm still not sure about how MainAS is connected to MainFLA. Is it implied? I guess it's part of being new to this lol.

    Also, regarding the code you just posted, what is actually doing the tracing to file?

    Thanks man

  11. #11
    Quote Originally Posted by epitomatic
    Appreciate it, however I may as well ask because i'm still not sure about how MainAS is connected to MainFLA. Is it implied? I guess it's part of being new to this lol.
    MainAS is the document class. A document class is like any other except that it must extend a display object and you have to set it as the document class in the fla file. You just type it in the text field in the properties panel.

    So, if your document class is called DocumentClass.as you just type "DocumentClass" in that text field. If the DocumentClass is in a folder named classes, you would type "classes.DocumentClass".

    Quote Originally Posted by epitomatic
    Also, regarding the code you just posted, what is actually doing the tracing to file?
    Can you explain what you mean by "tracing"?

  12. #12
    Ahh, thanks for that insight; I should have checked that!

    Quote Originally Posted by Mentalist View Post
    Can you explain what you mean by "tracing"?
    Sure, just whatever is being printed in the output box in flash during on runtime.

    Thanks

  13. #13
    Also uhm, do I need to run everything within the MainAS function:
    Code:
            
            private function createPlaylist($event:Event):void
            {
    Since that's after the XML is loaded?

    Thanks

  14. #14
    I'm creating instances of PlaylistItem, passing the title and artist strings to the constructor so that it can enter those values into the two text fields that I have set up.

    If you open the fla file and look in the library, you'll see PlaylistItem. Right click it and select "Linkage...". I have the class set to PlaylistItem, meaning it will use the external code that I have written in PlaylistItem.as.

    Open PlaylistItem up (the library item, not the class), and you'll see two layers; two text fields and a background. I named the top text field "titleText" and the bottom one "artistText". Open the PlaylistItem.as class and look at the code (note that I had to import flash.text.TextField), and you'll see it using the two instaces of TextField that I have placed on the stage in the fla file.

    Combining the IDE with coding in classes is very efficient, I find. You could just import TextField, create them in the class, set up all their properties and position them and all that; however, you're going to end up adding mabye 20+ lines of code to that class, and it will be hard to get it looking exactly how you want it to. If you're collaborating with a designer who won't understand your code, this is also a very effective method because he/she can just modify the look of the (in this example) playlist item without having to touch any code.

  15. #15
    Ahh, that's great; I appreciate that, although I feel it was a dumb question lol.

    Lastly, I posted this a little late but i'll just revisit it, do I need to post all my output code inside of the createPlaylist event?

    Thanks a bit

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012