Results 1 to 15 of 17
-
February 16th, 2009, 11:36 PM #110Registered User
posts
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.
Also, I wanted to mention that I have checked 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?!?!
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?
ThanksLast edited by epitomatic; February 16th, 2009 at 11:41 PM.
-
February 16th, 2009, 11:47 PM #256Registered User
postsIt'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); } }
-
February 16th, 2009, 11:51 PM #310Registered User
postsFirst 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
-
February 17th, 2009, 12:03 AM #456Registered User
postsWell, 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?
-
February 17th, 2009, 12:06 AM #510Registered User
postsAhh, 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
-
February 17th, 2009, 12:25 AM #656Registered User
postsI'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.
-
February 17th, 2009, 04:34 PM #710Registered User
postsHey, 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
-
February 17th, 2009, 04:42 PM #810Registered User
postsAhh, nevermind, I had Main.FLA opened lol; so I assume after this in MainAS:
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,Code:new URLLoader(new URLRequest("playlist.xml")).addEventListener(Event.COMPLETE, createPlaylist); addChild(loadingText);
Thanks
-
February 17th, 2009, 05:06 PM #956Registered User
postsIf 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:
So you can just have an int currentTrack or something and when you want to play the next track, just increase it.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); }
Code:var track:XML = playlistXML.track[trackNumber];
-
February 17th, 2009, 07:57 PM #1010Registered User
postsAppreciate 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
-
February 17th, 2009, 09:08 PM #1156Registered User
postsMainAS 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.
Originally Posted by epitomatic
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".
Can you explain what you mean by "tracing"?
Originally Posted by epitomatic
-
February 17th, 2009, 09:13 PM #1210Registered User
posts
-
February 17th, 2009, 09:28 PM #1310Registered User
postsAlso uhm, do I need to run everything within the MainAS function:
Since that's after the XML is loaded?Code:private function createPlaylist($event:Event):void {
Thanks
-
February 17th, 2009, 09:32 PM #1456Registered User
postsI'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.
-
February 17th, 2009, 09:40 PM #1510Registered User
postsAhh, 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

Reply With Quote


Bookmarks