Fix

Loading Multiple XML Files in Flash

Wednesday, April 21st, 2010

While working on my second version of RocFolio, I came across a few errors while trying to load multiple xml files in Flash AS3. When loading the files sometimes Flash would randomly put additional characters at the end of my xml file giving me parsing errors. So I had to figure out a way to cut those out, and I’ll try to explain real quick what I did.

So to load your XML you have to create your load and request variables and then you should create a few event listeners for progress and complete events, like this:

1
2
3
4
5
6
var loader:URLLoader = new URLLoader();
var requestURL:URLRequest = new URLRequest(xmlfilename.xml);
 
//create progress and complete event listeners for the loader, and load the urlrequest
loader.addEventListener(ProgressEvent.PROGRESS, getBytesLoop);
loader.addEventListener(Event.COMPLETE, loadXML);

Now in your loadXML function all you have to do is figure out where the xml file actually ends, and then index that spot and then cut the rest of the string out with the substring() command. Here is how you do that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//function loadxml loads the xml and handles assigning to places
function loadXML(e:Event):void {
 
	var dataString:String = e.target.data;
	var finalString:String = "";
	var endofString:uint;
 
        //find out where the end of the xml file is, however your set it up it should be the last closing tag, this one is </settings>
	endofString = dataString.indexOf("</settings>", 0);
 
	//cut out the good part of the xml using substring, then use 0 as your starting point to where you got your endOfString
        //make sure to add whatever amount you need to be at the end of the your last closing tag, </settings> has 11 characters in it so +11
	finalString = dataString.substring(0, endofString+11);
 
	//then assign the finalstring to your XML variable
	try {
		var theXML:XML = new XML(finalString);
	} catch (e:TypeError) {
		//loader.load(requestURL);
		trace("A TypeError has occurred : \r\t" + e);
	}
 
//then go on with setting up your XMLLists so you can access the data how you want
}

So hopefully this helps those out there that are getting weird parsing errors when trying to load multiple XML files. I know I tried finding the solution out on the web and couldn’t so that’s why I wrote this, if it helps you out please leave a comment.

Cya

Workaround Solution to Flash Error #2044, Unhandled SecurityError, and Error #2048, Security Sandbox Violation

Friday, February 26th, 2010

I sell Flash components on ActiveDen, and while working with a client on my Flash News Ticker we ran into the infamous “Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation” when trying to gather information from an external RSS Feed.

This error occurs when your flash file is trying to get data from a different domain and it can’t because of security reasons on their server. The usual fix for this is to create a crossdomain.xml file that allows access from other websites to use the data on yours, but the problem with that is if you don’t host that site you want the data from then how could you create or update the crossdomain.xml file on their server.

I did find a work around for it though! Not sure if its “the right way to do it” but it does work.

Here is the solution, I copy that url of the feed I want the data from, go to my feedburner account and create a feedburner feed of the same feed. If you don’t know what feedburner is then you should definatly check it out. You basically can sign up for feedburner if you have a google account already. Anyway, after you get the feedburner feed of the data you want you can view it. Then if you want to see the xml version of the Feedburner Feed, you can just add  “?format=xml” onto the end of the url they give you, and this is the normal RSS 2.0 feed that we are wanting.

This is the data you want, because its not on the security problematic website. The next thing I ran into was getting the images to display in the news ticker as well. I kept getting the same error but if I dismissed the errors then they still worked. So I used a try-catch clause to get the error and throw it if it gets one. Here is an example of that code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function imageLoaded(e:Event):void {
	try {
                //make the image into a bitmap and smooth it
		var image:Bitmap = e.target.content as Bitmap;
		image.smoothing = true;
 
		//resizes the image
		e.target.content.width = 55;
		e.target.content.height = 35;
 
	} catch(error:Error) {
		trace("Error catch: " + error);
	}
}

This is called from an event listener when loading it in using a loader object, that looks like this:

imageLoader.contentLoaderInfo.addEventListener(Event.INIT, imageLoaded);

So basically it worked after this, and the images even showed up without showing the error. The only problem is that the images don’t get resized because they can’t be loaded into the swf memory. Here are the examples: the one on the left is the broken example, it can’t get the information it wants to so it just keeps trying to load it. The example on the right shows the fix. (You have to view them in IE so the error warnings appear, another thing to hate about IE. :/ )

Any thoughts or comments are encouraged,
Cya

Actionscript 3 Fix for the Flash Fullscreen Problem

Tuesday, December 15th, 2009

I have come across an error while working with Flash Fullscreen. The problem occurs when you have certain publish settings. The publish setting are in the html tab -> Window Mode -> Transparent Windowless. So the SWF file is basically transparent like a PNG image. It has trouble knowing where the stage is on this setting. So I came up with  a work around or fix that takes care of this. To fix this you have to create a background movieclip that resizes to the stage size that is hidden as well, so its alpha is zero, so that Flash recoginzes that your mouse is still over the swf file, I know its weird but this works.

So add a movieclip to the stage that is the same size of the stage, but make the color’s alpha zero. Make sure the instance name is “bg” then in the actionscript add this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    //import library files
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
 
    //reset the background movieclip function
    resetBGsize();
 
    //set the stage scalemode and alignment to no scale and top left
    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.align = StageAlign.TOP_LEFT;
 
    // add an event listener to the stage for when it is resized
    stage.addEventListener(Event.RESIZE, onResizeStage);
 
    //onresizestage function for the event listener, calls resetBGsize
    function onResizeStage(e:Event):void {
       resetBGsize();
    }
 
    //resetbgsize function resizes the background movieclip and centers it
    function resetBGsize():void {
       bg.width = stage.stageWidth;
       bg.height = stage.stageHeight;
       bg.x = stage.stageWidth / 2 - bg.width / 2;
       bg.y = stage.stageHeight / 2 - bg.height / 2;
    }

Hope this helps someone out.

Cya