Jump to content
Search Community

Loading and Unloading external SWF - Child

AS3_Learning_Curve test
Moderator Tag

Recommended Posts

Hey there people,

 

Please help a complete as3 learner here in figuring out the basics.

 

I have used the example code

 

import com.greensock.*;

import com.greensock.loading.*;

import com.greensock.events.LoaderEvent;

import com.greensock.loading.display.*;

 

//STAGE ALIGNMENT AND POSITIONING FUNCTION

stage.align = StageAlign.TOP_LEFT;

stage.scaleMode = StageScaleMode.NO_SCALE;

stage.dispatchEvent(new Event(Event.RESIZE));

 

//create a LoaderMax named "mainQueue" and set up onProgress, onComplete and onError listeners

var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});

 

//append several loaders

//queue.append( new XMLLoader("xml/data.xml", {name:"xmlDoc"}) );

//queue.append( new ImageLoader("img/photo1.jpg", {name:"photo1", estimatedBytes:2400, container:this, alpha:0, width:250, height:150, scaleMode:"proportionalInside"}) );

queue.append( new SWFLoader("test.swf", {name:"childClip", estimatedBytes:3000, container:this, x:250, autoPlay:false}) );

queue.append( new SWFLoader("test2.swf", {name:"childClip2", estimatedBytes:3000, container:this, x:250, autoPlay:false}) );

//queue.append( new MP3Loader("mp3/audio.mp3", {name:"audio", repeat:2, autoPlay:true}) );

 

//prioritize the loader named "photo1"

//LoaderMax.prioritize("photo1"); //same as LoaderMax.getLoader("photo1").prioritize();

 

//start loading

queue.load();

 

function progressHandler(event:LoaderEvent):void {

trace("progress: " + event.target.progress);

}

 

function completeHandler(event:LoaderEvent):void {

var image_mc:ContentDisplay = LoaderMax.getContent("childClip2");

// PLACE IT ON STAGE

image_mc.x = 0;

image_mc.y = 100;

addChild(image_mc);

trace(event.target + " is complete!");

}

 

function errorHandler(event:LoaderEvent):void {

trace("error occured with " + event.target + ": " + event.text);

}

 

 

 

My project is timeline based. I can get the external swf files loaded and even add a child to the stage (as above).

 

Then is I wish to add and remove children from the loaded files elsewhere on the timeline.

 

How would I go about adding a new child and removing one. I come more from an art background so this is very confusing indeed.

 

I tried

 

addChild(image_mc);

removeChild(image_mc);

 

but it gives me "Access to undefined property image_mc"

 

Basically I would love to have a list of external swf files that load at the beginning of the fla. These could then be added and removed as I wish throughout my project. If this is not the best way to achieve this then please let me know. Also is there a way of telling if a specific swf has failed to load?. This way I may be able to replace the failed swf with an alternative graphic. Sorry for the bundle of question but I have a project working in the old way. I just thought that using loadermax could centralise some of the process.

 

Any ideas?

 

Regards

 

Ade

Link to comment
Share on other sites

the reason you can't access image_mc "later on" in your code is because you declare the image_mc var inside of a function.

 

when you do that the term image_mc can only be used inside that function.

 

this article on Variable Scope explains this issue in further detail:

 

http://help.adobe.com/en_US/ActionScrip ... 90204-7f8c

 

if you want to unload an image that LoaderMax has previously loaded, you can access it similar to the way that you did when you first defined image_mc.

 

each loader's content has a reference to its loader so you could do:

 

LoaderMax.getContent("childClip2").loader.unload();

 

or you could get to the loader more directly with:

 

LoaderMax.getLoader("childClip2").unload();

 

 

likewise to add previously loaded image to the display list

 

addChild(LoaderMax.getContent("childClip2"));

 

does that help?

Link to comment
Share on other sites

Carl,

 

I wondered if you have any ideas here to my scenario.

 

I have a web cam image running it timers for 5 minutes and then restarts then timeline and loadermax. I use the LoaderMax.getLoader("photo1").unload(); to unload the image.

 

It appears however that the image doesn't change until I shut down flash and then restart the web cam swf? Is there something I must do in order to flush out the image and have loadermax re-initialise / load the image again?.

 

Also is there a way of telling if an item fails to load what that item is ?. I wish to have a list page upon starting the swf that lists all the external swf to be loaded. If it fails I wish to display say a cross to let the user know that something is wrong and the file does not exist or has something wrong with it.

 

Is this possible?

 

Regards

 

Ade

Link to comment
Share on other sites

hi ade

 

I'm having trouble understanding

 

I have a web cam image running it timers for 5 minutes and then restarts then timeline and loadermax.

 

perhaps if I knew more what your camera was doing. is it uploading an image to a server? does the images name change?

 

if you are re-loading an image with the same name, I would use the unload() and also make sure you use the noCache setting.

 

you can read more about noCache here:

 

http://www.greensock.com/as/docs/tween/ ... oader.html

Link to comment
Share on other sites

Sorry to be so vague Carl.

 

The technical side of things is getting in the way of my art for the project. Greensock though is a god send, what a product.

 

I tried the no cache and indeed now the image refreshes whoppee.

 

Basically the work is for a school, I wish to have several buttons along the bottom of the screen. Each that will load up a different image from around the world. The images are web cam that the addresses don't change and the file names stay the same. It is a matter of clicking a button and the image appears. I would love it to be work like this. If the user clicks one of the buttons then the appropriate image loads and refreshes. If however the image is loaded and left running then after 5 minutes the image is automatically refreshed anyway. This way if the children leave it alone then the image will be current all day. I'm sure doing this will take me days and if someone has some similar code that I could cobble together then please do speak up it would make y life a lot easier at this point in time :).

 

To add more complexity I wish to have an image appear if for some reason the image not exits. Also is there a way to monitor if a file does not load for individual files? Say if an italian web cam fails I can put up an italian flag, german fails german flag etc? I'm sure once I have this loading principle cracked then I can apply it to any asset loading into a project.

 

It's a lot of question and I would appreciate the help.

 

Many thanks

 

Ade

 

Code I used so far loading a single image. It does however now refresh using the noCache:1 as instructed.

 

--

 

Frame 1

 

import com.greensock.*;

import com.greensock.loading.*;

import com.greensock.events.LoaderEvent;

import com.greensock.loading.display.*;

 

//create a LoaderMax named "mainQueue" and set up onProgress, onComplete and onError listeners

var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});

 

//append several loaders

//queue.append( new XMLLoader("xml/data.xml", {name:"xmlDoc"}) );

queue.append( new ImageLoader("http://www.campaniameteo.it/webcam/mercogliano/current.jpg", {name:"photo1", estimatedBytes:2400, container:this, alpha:0, noCache:1, width:640, height:480, scaleMode:"proportionalInside"}) );

//queue.append( new SWFLoader("swf/child.swf", {name:"childClip", estimatedBytes:3000, container:this, x:250, autoPlay:false}) );

//queue.append( new MP3Loader("mp3/audio.mp3", {name:"audio", repeat:2, autoPlay:true}) );

 

//prioritize the loader named "photo1"

LoaderMax.prioritize("photo1"); //same as LoaderMax.getLoader("photo1").prioritize();

 

//start loading

queue.load();

 

function progressHandler(event:LoaderEvent):void {

trace("progress: " + event.target.progress);

}

var image:ContentDisplay = LoaderMax.getContent("photo1");

function completeHandler(event:LoaderEvent):void {

 

TweenLite.to(image, 1, {alpha:1, y:0});

trace(event.target + " is complete!");

}

 

function errorHandler(event:LoaderEvent):void {

trace("error occured with " + event.target + ": " + event.text);

}

 

Frame 30

 

//PAUSE TIMER TO GO BEHIND VISUAL TIMER

stop();

trace("paused");

var millis_NATURECAM3:Number=30000;

var Tim_NATURECAM3:Timer = new Timer(millis_NATURECAM3, 1);

Tim_NATURECAM3.addEventListener(TimerEvent.TIMER, playit_NATURECAM3);

 

function playit_NATURECAM3(e:TimerEvent):void {

play();

}

 

Tim_NATURECAM3.start();

//Timer End

trace("play");

 

Frame 35

LoaderMax.getContent("photo1").loader.unload();

 

Frame 40

gotoAndPlay(1);

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