View Full Version : Newb is confused
chuckgrenade
02-15-2007, 02:15 AM
Yes. I'm confused. I can't figure out some of the actioscripting examples that are supposed top just be put into FLEX wrapped in the CDATA tag, right?
For example: I read a post that tells how to load xml with actionscript in FLEX but I get an error that the class cannot be nested.
package {
import flash.display.Sprite;
import flash.net.*;
import flash.events.*;
public class URLLoaderExample extends Sprite
{
public function URLLoaderExample()
{
var externalXML:XML;
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("xmlFile.xml");
loader.load(request);
loader.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
externalXML = new XML(loader.data);
trace(externalXML.toXMLString());
}
}
}
}
You put all that ine <mx:Script>, CDATA tags, right?!
What am I missing?
springframework
02-15-2007, 02:46 AM
add this line of code to your URLLoader
loader.dataFormat = URLLoaderDataFormat.TEXT;
and then access it inside the onComplete function with.
xml = new XML(event.target.data);
chuckgrenade
02-15-2007, 02:53 AM
I'm getting an error "Classes must not be nested" and it is pointing to the line of code:
public class URLLoaderExample extends Sprite
The XML actually loads fine without the additions to the code. My problem is that the app will not compile with this "classes" error. No change I have made will show up since I have had errors. :(
package {
import flash.display.Sprite;
import flash.net.*;
import flash.events.*;
public class URLLoaderExample extends Sprite
{
public function URLLoaderExample()
{
var externalXML:XML;
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("xmlFile.xml");
loader.load(request);
loader.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
externalXML = new XML(loader.data);
trace(externalXML.toXMLString());
}
}
}
}
springframework
02-15-2007, 03:17 AM
try not nesting your classes.
springframework
02-15-2007, 03:19 AM
you are also using the default package. it might not like that.
chuckgrenade
02-15-2007, 03:21 AM
... doesn't look nested to me.
Of course, I am not familiar with classes and packages and how they work. Care to explain, please? - or point me in the right direction?
chuckgrenade
02-15-2007, 03:49 AM
This is my code - the other code was an example given by adobe.
I get an error about the line:
public class URLLoaderExample extends Sprite
the error says:
Classes must not be nested
<mx:Script>
<![CDATA[
import flash.display.Sprite;
import flash.net.*;
import flash.events.*;
import mx.states.*;
public class URLLoaderExample extends Sprite
{
public function URLLoaderExample()
{
var externalXML:XML;
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://www.trapp.com/clients_XML.xml");
loader.load(request);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.TEXT;
function onComplete(event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
externalXML = new XML(loader.data);
xml = new XML(event.target.data);
trace(externalXML);
}
}
}
]]>
</mx:Script>
springframework
02-15-2007, 04:02 AM
Declaring a function inside a function means you are nesting that function.
this is not nested
public function URLLoaderExample()
{
var externalXML:XML;
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://www.trapp.com/clients_XML.xml");
loader.load(request);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.TEXT;
}
function onComplete(event:Event):void
{
externalXML = new XML(event.target.data);
trace(externalXML);
}
springframework
02-15-2007, 04:06 AM
I use flash 9 alpha so i dont fully understand flex 2, but i wouldn't think you should be declaring a class definition inside <mx:Script> CDATA tags.
I think <mx:Script> CDATA tags are for declaring functions and running actionscript.
you should create the class inside its own .as file.
then import that .as file
then you can create that an object of that class and call its meathods inside the <mx:Script> CDATA tags
The problem heres is that you have an MXML class (i can see that as you are using a Script tag). Your MXML will specify what is the superclass, yet inside the code (what should just be logic), you are defining abnother class, and another subclass.
To use that AS it needs to be an AS only class.
dr_zeus
02-15-2007, 04:40 PM
To use that AS it needs to be an AS only class.
In other words, you need to put your class URLLoaderExample class in a file called URLLoaderExample.as.
If you want to put ActionScript inside a <mx:Script> block, you should only use functions, no classes because your MXML file is a class. That's why you're getting the error about nested classes.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.