Jump to content
Search Community

How to rewrite for LoaderMax

failure13 test
Moderator Tag

Recommended Posts

Sorry for really stupid question, but i'm new to LoaderMax, and i really don't understand how to do simple things...

// create new loader for vr player
var loaderVR3:Loader = new Loader();
// spceify URL of 1st vr .swf to start tour
var vrURL3:String = "system/vr/1.2.swf";
// set vr request from vrURL
var vrURLreq3:URLRequest = new URLRequest(vrURL3);
// create container for vr
var vr3:MovieClip;
// start loading
timeoutID = setTimeout(startIt3, loadDelay)
function startIt3() {
// call initPanorama every frame
addEventListener(Event.ENTER_FRAME, initPanorama3);
// Tell the loader to call 'initHandler' after the swf is initialized
loaderVR3.contentLoaderInfo.addEventListener(Event.INIT, initHandler3);
// load video content from vrURL string
loaderVR3.load(vrURLreq3);
}
// function to cast 'DisplayObject' to 'MovieClip'
function initHandler3(event:Event):void {
vr3 = MovieClip(loaderVR3.content);
// name and add vr to the stage
loaderVR3.name = "loaderVR3"
addChild(loaderVR3);
// slowly change alpha of loader
TweenMax.to(loaderVR3, 1.8,{alpha: 1, delay: 0.06, onStart: loaderVR3.alpha = 0});
}
function initPanorama3(e:Event) {
// check if the panorama object is available and initialize it
if ((vr3 != null) && (vr3.pano != null)) {
	removeEventListener(Event.ENTER_FRAME, initPanorama3);
	vr3.x=260;
	vr3.y=50;
	onResize(null);
}
}

 

How would i rewrite this code, for LoaderMax, to keep it as simple, or even more simple? :D

Thx in advance!

Link to comment
Share on other sites

Sure, you code would probably look something like this:

 

var loaderVR3:SWFLoader = new SWFLoader("system/vr/1.2.swf", {onInit:initHandler3});
var vr3:MovieClip;
timeoutID = setTimeout(loaderVR3.load, loadDelay)

function initHandler3(event:Event):void {
vr3 = MovieClip(loaderVR3.rawContent);
loaderVR3.content.name = "loaderVR3"
loaderVR3.content.alpha = 0;
addChild(loaderVR3.content);
TweenMax.to(loaderVR3.content, 1.8, {alpha: 1, delay: 0.06});
if (vr3 != null && vr3.pano != null) {
	initPanorama3();
} else {
	loaderVR3.addEventListener(LoaderEvent.PROGRESS, initPanorama3);
}
}
function initPanorama3(e:Event) {
if (vr3 != null && vr3.pano != null) {
	loaderVR3.removeEventListener(LoaderEvent.PROGRESS, initPanorama3);
	vr3.x=260;
	vr3.y=50;
	onResize(null);
}
}

 

That improves upon what you had because it's not wastefully checking if vr3 != null on every frame during the load.

 

Keep in mind that LoaderMax can do a whole lot more than just stand in for a regular Loader object. You can build up a queue of things to load and get very fine-grain control of everything and report progress as a whole, etc.

 

Enjoy!

Link to comment
Share on other sites

Well yeah, it's just a beginning for me...

So i got problem with code you provided, it always placed at x = 0, y = 0, just as if you would remove enterFrame listener (which i of course would LOVE to remove, but once you remove it from original code - panorama places at x = 0, y = 0), it seem that's how it works.

So right now i'm trying to adopt your rewritten code for enterFrame listening, and still get same results yet...hm

 

P.S. what exactly .content and .rawContent means in this code?

Link to comment
Share on other sites

The docs explain content and rawContent, plus it's the first tip here:

http://www.greensock.com/loadermax-tips/

 

Docs are at http://greensock.com/as/docs/tween/_loadermax.html

 

Summary: rawContent is the equivalent to Loader.content in your old code. content is a special wrapper Sprite that LoaderMax uses to add a bunch of conveniences.

 

Are you saying that if you check vr3.x and vr3.y after you set them to non-zero values, they report incorrectly? Like if you do vr3.x = 260 and then trace(vr3.x) it doesn't report as 260?

 

I wonder if you're just misunderstanding what is getting placed on the stage (content, not rawContent). SWFLoader puts your subloaded swf's root (rawContent) into a ContentDisplay object (a Sprite) (also called "content") and THAT is the thing we were placing on the stage. So if you check the x position of that, it would of course be at the default of 0,0 unless you change it. Again, it's simply a Sprite. But if you change vr3's x/y inside the ContentDisplay wrapper, it should work fine. I'm a little confused about what problem you're describing here. Maybe it'd help if you posted a super simple example FLA.

 

There are some videos and written tutorials listed here:

http://www.greensock.com/learning/#loadermax

Link to comment
Share on other sites

I extremely apologise for not reading manual in firstplace...

I got a very very small time period to finish this, so totally got no time :(

Thank you for an explanation about .content and .rawContent!

 

Are you saying that if you check vr3.x and vr3.y after you set them to non-zero values, they report incorrectly? Like if you do vr3.x = 260 and then trace(vr3.x) it doesn't report as 260?

They doesn't even trace at all!

So somehow initPanorama3 ain't even ever executing with your code O_o

That probably means, that vr3 or vr3.pano is still null at that point...But then why?

 

Ahh...Well, i don't know even how to say.

This pano2vr api where i create panoramas - is extremely tricky.

It seem that it have to get some time to pass x / y / width / height , that's why they offer to use enterFrame on their site (which is huge and frustating waste of memory, even for a breath moment only)

 

To be honest - right now i don't understand completely how it works.

I have made super simple example for you, there are 2nd frame with your code, and 3rd with mine, to test and see difference - press main 3 to see how it should be placed (with my code), and press main 2 to see your example...But thing is, - once you remove enterFrame listener from my code - it's same result...

Hell i'm stuck \

 

http://www.sendspace.com/file/qr3pec

Link to comment
Share on other sites

It looks like the pano2vr stuff is poorly written. It's throwing this error:

ArgumentError: Error #2005: Parameter 0 is of the incorrect type. Should be type Filter.

at flash.display::DisplayObject/set filters()

at lensflare_fla::MainTimeline/doEnterFrame()

So apparently there's a method called doEnterFrame() that has some faulty code in it. I can't see it, of course, because I only have the main FLA you posted. And I really don't have time to dig into their code and figure out the problems there. But it does look like the errors are unrelated to LoaderMax or the code I posted (from what I can tell).

 

You can switch the PROGRESS listener to an ENTER_FRAME listener instead and that seems to work.

 

var loaderVR3:SWFLoader = new SWFLoader("outside.swf", {onInit:initHandler3});
var vr3:MovieClip;
timeoutID = setTimeout(loaderVR3.load, loadDelay)

function initHandler3(event:Event):void {
vr3 = MovieClip(loaderVR3.rawContent);
loaderVR3.content.name = "loaderVR3"
loaderVR3.content.alpha = 0;
addChild(loaderVR3.content);
TweenMax.to(loaderVR3.content, 1.8, {alpha: 1, delay: 0.06});
if (vr3 != null && vr3.pano != null) {
	initPanorama3(null);
} else {
	this.addEventListener(Event.ENTER_FRAME, initPanorama3);
}
}
function initPanorama3(e:Event) {
if (vr3 != null && vr3.pano != null) {
	this.removeEventListener(Event.ENTER_FRAME, initPanorama3);
	vr3.x=260;
	vr3.y=50;
	trace("setting x: "+vr3.x);
	vr3.pano.setWindowSize(400,300);
}
}

 

Apparently pano takes some time (at least a frame or two AFTER the entire swf has loaded) to get created.

 

By the way, this code is invalid inside your tween:

onStart: loaderVR4.alpha = 0

onStart is supposed to receive a function, but you're actually immediately setting alpha to 0 (literally on that line, not when the tween starts). I assume you meant to wrap that in a function like:

onStart: function() {loaderVR4.alpha = 0;}

But even that seems odd - why not use a fromTo() tween and simply define alpha:0 in the "from"? Or set it immediately before you even create the tween?

 

Anyway, I hope that helps.

Link to comment
Share on other sites

haha, well, this exact error is not pano2vr realted, it's poorly written sunlight graphix :D

So just ignore it...

 

Thx, this code seem to work that way!

Apparently pano takes some time (at least a frame or two AFTER the entire swf has loaded) to get created.

Exactly, that's why they used enter frame in first place it seem...

 

haha, well, about alpha i know :))

It's just stupid mistake that i've done when simplifing code :)

 

And now comes my most hardcore question...

Inside 1st frame of my test file, please find a function called cleanUp! :D

 

// cleanup of currently loaded elemnt
function cleanUp(e:Event):void {
   // CLEAN VIRTUAL REALITY
   for (var v:int = 1; v <= 2; v++) {
       // create temp vars for vr and loaderVR instances
       var vr:MovieClip = this["vr" + v] as MovieClip;
       var loaderVR:Loader = this["loaderVR" + v] as Loader;
       // check if vr exist on stage
       if (vr != null && vr.pano != null && vr.stage) {
           // remove vr instance
           vr.pano.bmpTile[0].dispose();
           vr.pano.bmpTile[0] = null;
           vr.pano.bmpTile[1].dispose();
           vr.pano.bmpTile[1] = null;
           vr.pano.bmpTile[2].dispose();
           vr.pano.bmpTile[2] = null;
           vr.pano.bmpTile[3].dispose();
           vr.pano.bmpTile[3] = null;
           vr.pano.bmpTile[4].dispose();
           vr.pano.bmpTile[4] = null;
           vr.pano.bmpTile[5].dispose();
           vr.pano.bmpTile[5] = null;
           loaderVR.mouseChildren = false;
           vr.pano.cleanup();
           loaderVR.unloadAndStop();
           removeChild(loaderVR);
           vr.pano = null;
           trace ("vr" + v + " cleanup done");
           // kill all sound
           SoundMixer.stopAll();
       }
}

As you can see it is extremely hard to clean virtual reality, coz it have at least 6 heavy bitmaps (faces of cube), which need to be disposed and nullified...and only then i should unloadAndStop, and finally remove .swf, afterwords killing it's own sound %)

 

Mess, ain't it?

My question is of course - how should i unload such a crazy stuff with loaderMax, if i'll use code you've provided above to add it on stage?

And the most interesting part i would like to know about loaderMax, is it's benefits of unloading content, compared to native flashes loader...I want to always clean all the loaded mess when it's not needed, memory is something i want to keep low with every project, but it's so hard sometimes...

Link to comment
Share on other sites

Hi,

 

Sorry to be getting into this late. I haven't grasped your complex code in its entirety so I can't tell you exactly how to clean everything up.

 

LoaderMax and each of its loader types (ImageLoader, SWFLoader, MP3Loader, etc) have a dispose() method. Please read the documentation:

 

http://www.greensock.com/as/docs/tween/com/greensock/loading/core/LoaderCore.html#dispose()

 

You will see that this method takes care of flushing content and making the loader eligible for garbage collection.

 

In the case of your SWFLoader you could do:

 

 

loaderVR3.dispose(true);

 

I'm sure as you take the time to read more of the documentation you will see that LoaderMax and its related tools offer many conveniences not found in the native AS3 Loaders or any other 3rd party loading libraries.

Link to comment
Share on other sites

Just to be clear, you should clean up after yourself in your subloaded swf before calling dispose() because it would be impossible for LoaderMax to stop sounds that you started or close NetStreams or remove listeners that you created inside that swf. So from your sample code, it looks like you might need to do some cleanup first. But the SWFLoader's dispose() method essentially takes the place of your old Loader.unloadAndStop() call.

Link to comment
Share on other sites

As i understood, with dispose() command, i won't need to do those:

loaderVR.mouseChildren = false;

 

Right?

 

So in theory i should do all the cleanup processes, and then fire dispose, this should work:

function cleanUp(e:Event):void {

for (var v:int = 1; v <= 2; v++) {

// create temp vars for vr and loaderVR instances

var vr:MovieClip = this["vr" + v] as MovieClip;

var loaderVR:SWFLoader = this["loaderVR" + v] as SWFLoader;

// check if vr exist on stage

if (vr != null && vr.pano != null && vr.stage) {

// remove vr instance

vr.pano.bmpTile[0].dispose();

vr.pano.bmpTile[0] = null;

vr.pano.bmpTile[1].dispose();

vr.pano.bmpTile[1] = null;

vr.pano.bmpTile[2].dispose();

vr.pano.bmpTile[2] = null;

vr.pano.bmpTile[3].dispose();

vr.pano.bmpTile[3] = null;

vr.pano.bmpTile[4].dispose();

vr.pano.bmpTile[4] = null;

vr.pano.bmpTile[5].dispose();

vr.pano.bmpTile[5] = null;

vr.pano.cleanup();

vr.pano = null;

SoundMixer.stopAll();

loaderVR.dispose(true);

removeChild(loaderVR.content);

trace ("vr" + v + " cleanup done");

}

}

}

 

But it's not! :(

Doesn't remove instance from stage + doesn't seem to uload anything according to my memory checker class...

 

Sorry for putting this through you guys, it's really heavy stuff with this panorama...I can't hardly understand their logic myself...

But at least, i see that even with LoaderMax menu button animation lags when you load pano (and this is just example, original is super heavy..)

So i can't think nothing more than specifiy delay before load pano (so the animation would gone, and only then pano would load)

Do you got something else in mind to offer about getting rid of such lags?

Link to comment
Share on other sites

If you're asking if we know of a way to make Flash handle animations flawlessly while also loading a heavy-duty virtual reality swf that has its own demanding ActionScript running, no, unfortunately I'm not aware of any silver bullets for that :)

 

As for your garbage collection issues, it's very difficult to say because when I try to replicate it in the files you sent me, I cannot because there are errors in your sub-swf. Not only do I get that one about:

ArgumentError: Error #2005: Parameter 0 is of the incorrect type. Should be type Filter.

at flash.display::DisplayObject/set filters()

at lensflare_fla::MainTimeline/doEnterFrame()

But when I try to run your cleanup code, I also get errors about there not being a "dispose()" method on your bmpTile[] objects. TypeError: Error #1006: dispose is not a function. So I wonder if that's what's causing things not to be cleaned up as you hoped.

Link to comment
Share on other sites

Ah...It's too bad that there are no ways to stop this kind of lags :(

Well anyway, thx!

 

I think that dispose have problems, coz with my code i firstly need to define loader as a Loader object, at first for native it was like:

var loaderVR:Loader = this["loaderVR" + v] as Loader;

 

Now i have rewritten it ofr SWFLoader:

var loaderVR:SWFLoader = this["loaderVR" + v] as SWFLoader;

 

I don't got this error:

TypeError: Error #1006: dispose is not a function.

But probably this is due to some differences between native Loader dispose() behaviour, and LoaderMax dispose() behaviour?

 

Let me clear how i do it...

You should find cleanUp function in 1st frame and completely replace it with this:

function cleanUp(e:Event):void {

// CLEAN VIRTUAL REALITY

for (var v:int = 1; v <= 2; v++) {

// create temp vars for vr and loaderVR instances

var vr:MovieClip = this["vr" + v] as MovieClip;

var loaderVR:SWFLoader = this["loaderVR" + v] as SWFLoader;

// check if vr exist on stage

if (vr != null && vr.pano != null && vr.stage) {

// remove vr instance

vr.pano.bmpTile[0].dispose();

vr.pano.bmpTile[0] = null;

vr.pano.bmpTile[1].dispose();

vr.pano.bmpTile[1] = null;

vr.pano.bmpTile[2].dispose();

vr.pano.bmpTile[2] = null;

vr.pano.bmpTile[3].dispose();

vr.pano.bmpTile[3] = null;

vr.pano.bmpTile[4].dispose();

vr.pano.bmpTile[4] = null;

vr.pano.bmpTile[5].dispose();

vr.pano.bmpTile[5] = null;

vr.pano.cleanup();

vr.pano = null;

SoundMixer.stopAll();

loaderVR.unload();

loaderVR.dispose(true);

removeChild(loaderVR.content);

trace ("vr" + v + " cleanup done");

}

}

}

 

Then for convinence you can create new frame, and give it a name mainMenu1, so that when you create .swf with new code, if you press mainMenu2 to load your LoaderMax code - you should be able to clean it by pressing mainMenu1.

 

Sorry, probably it's very off for a simple example, but i can't really simplify it much! %)

 

Please ignore filters / enterframe error, it's bad written sunray (if you load vr and turn left you'l see it)

It have nothing to do with load / unload.

Link to comment
Share on other sites

I had already tried your cleanup() method and made the necessary alterations to call the SWFLoader's dispose() method instead of the Loader's unloadAndStop(), but I was still getting those errors about there not being a dispose() method on your pano.bmpTile[...] objects. If you want me to look at a sample FLA, please zip it and upload it here. The simpler the better - please remove all unnecessary code.

 

By the way, you don't need to call removeChild(loaderVR.content); because the dispose() automatically does that for you. In fact, calling that would likely cause an error because you're calling it after dispose(), so it would no longer be a child thus an error would be thrown.

Link to comment
Share on other sites

I'm confused. I downloaded and tried the fla you linked to and:

 

1) Your cleanUp() function never actually does anything because you're looping through objects that don't exist. Add trace(vr) under where you define that variable and you'll see that it's null both times through the loop.

 

2) You still left the removeChild(loaderVR.content) in there even though I told you that'd likely cause an error, seeing as how dispose() automatically removes it for you, thus it wouldn't be a child anymore.

 

3) You haven't addressed what I said about the bmpTile[...] objects not having a dispose() method.

Link to comment
Share on other sites

Man, i'm extremely sorry!

I was in rush, so forgot to change those things...

 

Here's working example, check it out

http://www.sendspace.com/file/i7pbep

 

1. In debug mode of player starting memory report 5,7, in non-debug mode 5,6.

2. When you load main menu 2 in debug mem 21,7, in non debug 32,97 (how's that? Debug usually use much more memory, and here everything is just reversed)

3. When you press main 1 to cleanup, debug mode 11,7, non-debug 21,10.

 

So question is, why such a strange difference comes between debug and non-debug memory?

And seconde question, why if you keep press main menu 2, memory increase for ~0,5 on each press?

Do you think that all the pano content is unloaded this way and there are no leaks from it?

 

You haven't addressed what I said about the bmpTile[...] objects not having a dispose() method.

I'm not sure what you're talking about exactly...It doesn't have error, and in fact this is the way they delete it on their site with native loader..

If you wanna check out memory without it, just delete those lines

vr.pano.bmpTile[0].dispose();

vr.pano.bmpTile[0] = null;

vr.pano.bmpTile[1].dispose();

vr.pano.bmpTile[1] = null;

vr.pano.bmpTile[2].dispose();

vr.pano.bmpTile[2] = null;

vr.pano.bmpTile[3].dispose();

vr.pano.bmpTile[3] = null;

vr.pano.bmpTile[4].dispose();

vr.pano.bmpTile[4] = null;

vr.pano.bmpTile[5].dispose();

vr.pano.bmpTile[5] = null;

In this case, without thiis code, memory would stay almost exactly the same...This 6 tiles are pano itself, most heavy content in this .swf

Link to comment
Share on other sites

1) I can't speak to how Flash's debugger manages memory exactly or why things are different - you'd need to ask Adobe about that. I wouldn't worry too much about it (personally)

 

2) I don't see any strange behavior. It is normal for memory to increase a bit over time and eventually go back down because objects build up a bit, like Event instances for ENTER_FRAME (and others). I clicked on menu2 and menu1 back and forth many many times and memory acted as expected - it raised when the sub-swf was loaded and then it went back down to almost the same memory. Again, it was very slightly more sometimes but then eventually it'd go back down.

 

So I'm not seeing any strange issues here besides the errors that are being thrown by that 3rd party VR tool. Are you still seeing something that is concerning you?

Link to comment
Share on other sites

No i think everything fine then.

I mean, obviosly i'm just a starting as3 programmer, and you know much more, so i come across to a friendly person that have developed best tween engine we can have for flash and java :)

So basicly just wanted to make sure, coz in perfect World i would think that if you load something up, than when you unload it it should unload exactly same amount of memory byte-to-byte, but i guess we can leave it for perfect World, right? :D

 

I think i have know everything i need yet for the LoaderMax, i will certanly read the documentation much more, and probably will use it against native loader, since it's much more easy to create it and define sometimes!

 

P.S. Can you recommend me a good, examples of LoaderMax codes for loading images / video (against netstream i guess, or it still use netstream?) and xml?

I'm just wonder to know "appropriate" use of it, than i would start to develop my own things with joy i guess! :)

P.P.S. i just loose this project, for no reason, so right now i open for some steady rtfm to know even more possibilities :D

Link to comment
Share on other sites

Hi Failure13.

 

Please see the LoaderMax resources here:

http://www.greensock.com/learning/

 

If you go through the 2 tutorials by me (Carl Schooff) you will have a strong understanding of the various shared properties and events that all the LoaderMax tools use.

 

You will then be able to cruise through Rich Shupe's excellent series and really understand it all on a deeper level.

 

After watching the Zync Interactive series you should have very few remaining questions;)

 

LoaderMax is an amazingly powerful and robust tool with lots of features and plenty of different ways to use it. After a little time you will see how each of the tools shares similar features and you will be able to jump from a SWFLoader to MP3Loader with ease. Once you grasp how the XMLLoade works, you'll never consider another tool for loading... I promise;)

 

By far the best thing for me when learning LoaderMax and the VideoLoader was the activeTuts+ tutorial:

http://active.tutsplus.com/tutorials/actionscript/smart-as3-video-loading-with-greensock-loadermax-free-active-premium/

 

Enjoy!

 

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...