Jump to content
Search Community

Loading unknown file types from flashvars

mhulse test
Moderator Tag

Recommended Posts

Hi,

 

My latest project loads images (jpg/png/gif) and/or swfs... In other words, the file type will be unknown until the time of the load.

 

Just curious what ya'll think would be the best way to handle the above situation when it comes to using LoaderMax?

 

I have no probs getting the variables from the flashvars, I am just wondering what the best way to handle calling the necessary loaders.

 

How would you handle this?

 

Thanks!

Micky

Link to comment
Share on other sites

Whoa! That rocks! Thanks so much for your quick reply and pro help!!!!

 

LoaderMax.parse() is way easier than the extension check/parsing code I just wrote! :D

 

Thank you Greensock! I love your code! :)

 

Have a great day.

 

Cheers,

Micky

Link to comment
Share on other sites

Yes, LoaderMax.parse() rocks.

 

I just last night posted a tutorial on validating image filenames prior to calling parse() to avoid run-time errors.

 

I was working on a little applet that loaded images that users uploaded to twitter and some had horrible file extensions or missing extensions which would throw a monkey wrench at parse() as it couldn't possibly know what loader typers to create if an extension was funky or missing.

 

it seems your flashvars are pretty solid, but if you ever get into the situation where you don't control how the filenames are formatted I found this regular expression to work really well in weeding out the troublesome files prior to sending them into parse().

//create regexp
var validImageRegExp:RegExp =/(?i)\.(jpg|png|gif|jpeg)$/

//these strings are valid and pass the test:
//they will output : true
trace(validImageRegExp.test("something.jpg"))
trace(validImageRegExp.test("something.PNG"))
trace(validImageRegExp.test("something.JPEG"))
trace(validImageRegExp.test("something.gif"))

//these strings are not valid and fail the test:
//they will output : false
trace(validImageRegExp.test("something.bmp"))
trace(validImageRegExp.test("something.jpe"))
trace(validImageRegExp.test("something"))
trace(validImageRegExp.test("something.jpg.crappy.rem"))

 

here is the long explanation:

http://www.snorkl.tv/2011/04/how-to-val ... r-project/

 

enjoy LoaderMax

 

Carl

Link to comment
Share on other sites

Whoa! Carl! That rocks too! :D

 

Thanks!!!!

 

That's a nice little RegExp! Great article too!

 

Not only will it help me for the situation you describe, I think I can utilize it on the other end of the spectrum:

 

// Note: "assets" is array of two files to load.
// Now parse all of the urls, creating a LoaderMax that contains the correct type of loaders (an ImageLoader, XMLLoader, SWFLoader, and MP3Loader respectively)
_loaders = LoaderMax.parse(assets, { name:'mainQueue', onComplete:completeHandler }) as LoaderMax;
// Begin loading:
_loaders.load();
...
...
private function completeHandler(e:LoaderEvent):void {
var validImageRegExp:RegExp =/(?i)\.(jpg|png|gif|jpeg)$/
var loaders:Array = _loaders.getChildren();
trace(validImageRegExp.test(loaders[1].rawContent))
}
...
...

 

Do you think that would be a decent way to test if I loaded an SWF and/or bitmap?

 

In my case, I just need to test if a couple of loaded files are bitmaps (gif/jpg/png) and/or swf... If it is a bitmap, I can skip all the timeline AS that I would normally use on controlling the timeline of loaded swf asset.

 

Either way, I will definitely be using your code to make sure the incoming flashvars are legit file types! Thanks so much for sharing your code, I really appreciate it! :)

 

Have an awesome day!

 

Cheers,

Micky

Link to comment
Share on other sites

One last question... Seems related to my original question...

 

When using:

 

_loaders = LoaderMax.parse(assets, { name:'mainQueue', onComplete:completeHandler }) as LoaderMax;

 

Should I do any GC after the onComplete handler has been called?

Link to comment
Share on other sites

Hi Micky,

 

very glad you found that code and the article useful!

 

as for your first question this is probably the best way to detect what type of Loader fired the onComplete.

 

function completeHandler(event:LoaderEvent):void {
   if (event.target is ImageLoader) {
       trace("this is an image: " + event.target);
   } else if (event.target is VideoLoader) {
       trace("this is a video: " + event.target);
   } else if (event.target is SWFLoader) {
       trace("this is an swf: " + event.target);
   }


}

//code taken from GreenSock

viewtopic.php?f=6&t=3927&p=15446&hilit=onCOmplete+determine+loader+type#p15488

 

 

 

As for the GC if you are completely done with your LoaderMax, yeah you could dispose() it so it gets cleaned up all nice.

 

Carl

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...