Jump to content
GreenSock

mico86

Load one time on click

Recommended Posts

Hello, congratulations for the project and the forum! how can I prevent a swf that loads every time I click on the button? I have a site that loads of SWFLoader via the menu buttons. Unfortunately though I am already in the SWF loaded and I click on the same menu button that loads the SWF, it is loaded again instead of being ignored. How can I do?

I tried to set a variable oldLoader and compare it with an if statement: if (oldLoader! = NewLoader) {} but does't work ... Thanks

Link to comment
Share on other sites

hmm, once a SWFLoader loads, requesting it to load again should not do anything.

The SWFLoader knows that the swf is already loaded and basically ignores the request.

 

Its tough to say what the problem is without seeing your code.

 

Are you creating a new SWFLoader every time you press the button?

 

Also what you can do is check the status of any loader. A status of 2 means it has completely loaded. so although its not necessary, you can do

 

if(myLoader.status != 2){
myLoader.load();
}

 

You really shouldn't need that though. Something like this will make a SWFLoader load when a button is pressed and it will only load once:

 

var swf:SWFLoader = new SWFLoader("theSwf.swf", {onProgress:progressHandler, onComplete:completeHandler, container:this});


function completeHandler( e:LoaderEvent ):void{
trace("complete");
}

function progressHandler( e:LoaderEvent ):void{
trace("progress");
}

stage.addEventListener(MouseEvent.CLICK, loadSwf);
function loadSwf(e:MouseEvent):void{
  trace(swf.status);
  swf.load();
}

 

You will see that once the swfLoads, it won't blink or refresh on multiple stage clicks. Also the status will be 0 on the first click and 2 on the subsequent clicks.

 

Also the progress won't fire on subsequent clicks.

Link to comment
Share on other sites

thank you very much! I'm noob, sorry :)

I'm doing a site of 3 external swf with unique menu for all always on top. How do I set the 3 swf always in the background? I created a variable var oldLoader: SWFLoader = null;

For now I have written this code (this is only for "bio_bt" in the external class of main swf container), but unfortunately does not work well! : (

  bio_bt.addEventListener(MouseEvent.CLICK, onMouseClick0);
  function onMouseClick0(e:MouseEvent):void {
  var BioLoader:SWFLoader = new SWFLoader("bio.swf", {name:"BioLoad", container:this, alpha:0, onComplete:completeHandler2, onProgress:progressHandler2});  
addChildAt(BioLoader.content,0);
BioLoader.load();
  function progressHandler2(event:LoaderEvent):void {
  //Display the % loaded in textfield
  preload_bg.visible = true;
  addChild(preload_bg);
  addChild(perc_txt);
  TweenLite.to(preload_bg,0.5,{x:stage.stageWidth, y:stage.stageHeight/2, alpha:1});
  TweenLite.to(perc_txt,0.5,{x:stage.stageWidth-148, y:stage.stageHeight/2-33, alpha:1});
  perc_txt.text=Math.ceil(event.target.bytesLoaded/event.target.bytesTotal * 100) + "%";
  }

  function completeHandler2(event:LoaderEvent):void {
  removeChild(perc_txt);
  removeChild(preload_bg);
  TweenLite.to(event.target.content, 1, {alpha:1});
  var mcTemp:MovieClip = BioLoader.rawContent
  oldLoader.unload();
  removeChild(oldLoader.content);
  oldLoader=BioLoader;
  trace(event.target + " is complete!");
  trace("La posizione di BioLoader è: " + getChildIndex(BioLoader.content));
  }
  }

 

How can I do to make all entries correctly? please help me, there are days that I'm puzzling: D

Link to comment
Share on other sites

you want to be careful where you place your code. right now inside of onMouseClick0() you are

-creating your loader

-creating the complete handler

-creating the progress handler

 

notice how the bioloader and the 2 handler functions are INSIDE of onMouseClick?

 

 

function onMouseClick0(){

[font=Consolas,]
[left]var BioLoader:SWFLoader = new SWFLoader("bio.swf", {name:"BioLoad", container:this, alpha:0, onComplete:completeHandler2, onProgress:progressHandler2});  [/left]
[/font]
[font=Consolas,]
[left] addChildAt(BioLoader.content,0);[/left]
[/font]

function[color=#5A5A5A][font=Consolas,] [/font][/color]completeHandler2(event:LoaderEvent):void[color=#5A5A5A][font=Consolas,] [/font][/color]{

}

function[color=#5A5A5A][font=Consolas,] [/font][/color]progressHandler2(event:LoaderEvent):void[color=#5A5A5A][font=Consolas,] [/font][/color]{

}

}

 

you really should be doing:

 


[font=Consolas,]
[left]var BioLoader:SWFLoader = new SWFLoader("bio.swf", {name:"BioLoad", container:this, alpha:0, onComplete:completeHandler2, onProgress:progressHandler2});  [/left]
[/font]


function onMouseClick0(){


bioLoader.load();

}
function completeHandler2(event:LoaderEvent):void {
   }
function progressHandler2(event:LoaderEvent):void {
   }

 

I don't know if that will solve all of your problems, but it is a good start.

 

This is a good article to read concerning how variable scope works:

 

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9d.html

Link to comment
Share on other sites

The site usually loads the menus and content. When I click for example on bio should remove "home" and load the contents of "bio". When I click on "disco", should remove the contents of organic or home and load the "disco".

How can I design this structure? I created a file containing the menu index.swf (loaded in the foreground) and loads home.swf in the background. Now when I click on "organic" content overlaps with that of home. I would like to unload and bio home.swf did take his place.

http://imageshack.us/photo/my-images/84/examplesite.jpg/

Link to comment
Share on other sites

Great!

But my the problem is that I want a preloader that it loaded over the content already loaded (and this I get it already), so as not to leave you with empty content waiting to load. Only when the new content will be uploaded, will be replaced by the old. But now this happens: I click for example on "Home", the preloader (preload_bg perc_txt for graphics and for the loading rate), it loads the contents of "Home", but now if I click on "Bio" disappears "Home "appears and the preloader. I would like instead of Home vanish only when the preloader is 100%

This is my code now:

   home_bt.addEventListener(MouseEvent.CLICK, onMouseClick);
  bio_bt.addEventListener(MouseEvent.CLICK, onMouseClick);
  contatti_bt.addEventListener(MouseEvent.CLICK, onMouseClick);
  var ContentLoader:SWFLoader = new SWFLoader("",{name:"ContContainer", container:this, onProgress:progressHandler, onComplete:completeHandler});

  function progressHandler(event:LoaderEvent):void {
  //Display the % loaded in textfield
  preload_bg.visible = true;
  addChild(preload_bg);
  addChild(perc_txt);
  TweenLite.to(preload_bg,0.5,{x:stage.stageWidth, y:stage.stageHeight/2, alpha:1});
  TweenLite.to(perc_txt,0.5,{x:stage.stageWidth-148, y:stage.stageHeight/2-33, alpha:1});
  perc_txt.text=Math.ceil(event.target.bytesLoaded/event.target.bytesTotal * 100) + "%";
  }

  function completeHandler(event:LoaderEvent):void {
  removeChild(perc_txt);
  removeChild(preload_bg);
  TweenLite.to(event.target.content, 1, {alpha:1});
  //var mcTemp:MovieClip = ContentLoader.content;
  trace(event.target + " is complete!");
  trace("La posizione di ContentLoader è: " + getChildIndex(ContentLoader.content));
  }

function onMouseClick(event:MouseEvent){

 if(event.target.name =="home_bt")
 {
  ContentLoader.url = "index2.swf";
 }
 if(event.target.name == "bio_bt")
 {
  ContentLoader.url = "bio.swf";
 }
 if(event.target.name == "contatti_bt")
 {
  ContentLoader.url = "contatti.swf";
 }
addChildAt(ContentLoader.content,0);
ContentLoader.load();
}

Link to comment
Share on other sites

in that case, re-using the same SWFLoader will not be appropriate.

I would suggest you create all of you loaders in advance.

Only load them when needed.

Perhaps append them all to a LoaderMax

Every time something loads, use a var like previousLoader to store a reference to it.

When you load a new item, wait for the onComplete callback to happen and then tell the previousLoader to unload().

 

I can't set up all that code for you but you would make sure each SWFLoader uses the same onComplete callback

 

var previousLoader:SWFLoader

function completeHandler(e:LoaderEvent):void{

//the first time you load something, there will be no previousLoader  
//make sure a previousLoader exists.

if(previousLoader){
	  previousLoader.unload();
}
 previousLoader = e.target
}

 

if you give each SWFLoader the same name as its respective button

 

var swf1:SWFLoader = new SWFLoader("bio.swf", {name:"bio_bt"});
var swf2:SWFLoader = new SWFLoader("index2.swf", {name:"home_bt"});

 

you could then do:

 

function onMouseClick(event:MouseEvent){
//get rid of all those ifs
//create a reference to the SWFLoader you should load

	var swf:SWFLoader = LoaderMax.getLoader( event.target.name);
//load it
	swf.load();
//add its content to the stage
	addChild(swf.content);

}

  • Like 1
Link to comment
Share on other sites

great solution! But I get an error 1118 on previousLoader = e.target

Implicit coercion of a value with static type Object to a type com.greensock.loading: SWFLoader that may not be related.

this is my complete code:

 

   
  var swf1:SWFLoader = new SWFLoader("bio.swf", {name:"bio_bt",onComplete:completeHandler});	
  var swf2:SWFLoader = new SWFLoader("contatti.swf", {name:"contatti_bt",onComplete:completeHandler});	

var previousLoader:SWFLoader
function completeHandler(e:LoaderEvent):void{
//the first time you load something, there will be no previousLoader 
//make sure a previousLoader exists.
if(previousLoader)
{		 
 previousLoader.unload();
}	
previousLoader = e.target.content; //I try with content and compile correctry but unload of previousLoader doesn't work 
}


  bio_bt.addEventListener(MouseEvent.CLICK, onMouseClick);
  contatti_bt.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(event:MouseEvent)
{
 var swf:SWFLoader = LoaderMax.getLoader(event.target.name);
 swf.load();
 addChildAt(swf.content,0);	 
}

Link to comment
Share on other sites

sorry for the trouble. try replacing the troublesome line with:

 

 

previousLoader = e.target as SWFLoader;

  • Like 1
Link to comment
Share on other sites

YOU ARE MY FU**** ANGEL!

This is my complete code, with preload function and animation. I hope it can serve some desperate like me who has lost his head for days!

 

var swf1:SWFLoader = new SWFLoader("bio.swf", {name:"bio_bt",onProgress:progressHandler,onComplete:completeHandler});  
 var swf2:SWFLoader = new SWFLoader("contatti.swf", {name:"contatti_bt",onProgress:progressHandler,onComplete:completeHandler});

  var previousLoader:SWFLoader
  function progressHandler(event:LoaderEvent):void {
  //Display the % loaded in textfield
  preload_bg.visible = true;
  addChild(preload_bg);
  addChild(perc_txt);
  TweenLite.to(preload_bg,0.5,{x:stage.stageWidth, y:stage.stageHeight/2, alpha:1});
  TweenLite.to(perc_txt,0.5,{x:stage.stageWidth-148, y:stage.stageHeight/2-33, alpha:1});
  perc_txt.text=Math.ceil(event.target.bytesLoaded/event.target.bytesTotal * 100) + "%";
  }

 function completeHandler(e:LoaderEvent):void{
	//the first time you load something, there will be no previousLoader
	//make sure a previousLoader exists.
 TweenLite.to(e.target.content, 1, {alpha:1});
	if(previousLoader)
	{
		 if(previousLoader != e.target) //This is to ensure that the contents do "unload" if you click the button again active
	{
	 previousLoader.unload();
		}
	}	  
	previousLoader = e.target as SWFLoader;
	TweenLite.to(preload_bg,0.5,{x:stage.stageWidth+180, y:stage.stageHeight/2, alpha:1});
 TweenLite.to(perc_txt,0.5,{x:stage.stageWidth, y:stage.stageHeight/2-33, alpha:1});
 removeChild(perc_txt);
 removeChild(preload_bg);
 }



  bio_bt.addEventListener(MouseEvent.CLICK, onMouseClick);
  contatti_bt.addEventListener(MouseEvent.CLICK, onMouseClick);
	function onMouseClick(event:MouseEvent)
	{
	 var swf:SWFLoader = LoaderMax.getLoader(event.target.name);
	 swf.load();
	 addChildAt(swf.content,0);	  
	}

Link to comment
Share on other sites

like me who has lost his head for days!

 

i know the feeling. glad you got it worked out. thanks for posting the code in its entirety.

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