Jump to content
Search Community

onComplete event does not fired

TheYellowGuy test
Moderator Tag

Recommended Posts

Hey, I know this tweening engine is the best. But, until yesterday i found something weird, the onComplete event does not fire.

 

here is my code:

import flash.events.MouseEvent;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import com.greensock.TweenMax;

stop();
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var basePath:String = "projects/";
var projectName:String = "Mandiri/";
var theSWF:String = "";

banners.banner1.addEventListener(MouseEvent.CLICK, onClick);
banners.banner2.addEventListener(MouseEvent.CLICK, onClick);
banners.banner3.addEventListener(MouseEvent.CLICK, onClick);
banners.banner4.addEventListener(MouseEvent.CLICK, onClick);

function onClick(me:MouseEvent):void{
	//trace(sub_container.numChildren);
	if(sub_container.numChildren > 0){
		TweenMax.to(sub_container.getChildAt(0), 1, {alpha:0, onComplete:clearChildren, onCompleteParams:["Yayy"]});
	}
	switch(me.currentTarget.name){
		case "banner1":
			theSWF = "01.swf";
		break;
		case "banner2":
			theSWF = "02.swf";
		break;
		case "banner3":
			theSWF = "03.swf";
		break;
		case "banner4":
			theSWF = "04.swf";
		break;
	}
	var swfLoader:Loader = new Loader();
	swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadSubComplete);
	swfLoader.load(new URLRequest(basePath + projectName + theSWF));
}
function clearChildren(param):void{
	trace(param);
	TweenMax.killTweensOf(sub_container);
	sub_container.removeAllChildren();
	trace(sub_container.numChildren);
}
function onLoadSubComplete(ev:Event):void{
	var theLoaded:MovieClip = MovieClip(ev.target.content);
	theLoaded.x = -theLoaded.width/2;
	theLoaded.y = -theLoaded.height/2;
	sub_container.addChild(theLoaded);
	TweenMax.to(theLoaded, 1, {alpha:1});
}

stage.addEventListener (Event.RESIZE, resizeListener);

function resizeListener(ev:Event):void{
	sub_container.x = stage.stageWidth/2;
	sub_container.y = stage.stageHeight/2;
	banners.x = stage.stageWidth/2 - banners.width/2;
}

DisplayObject.prototype.removeAllChildren = function(){	
	for(var j = this.numChildren-1; j >= 0; j--){
		this.removeChildAt(j);
	}
}

 

If you see that onComplete:clearChildren, onCompleteParams:["Yayy"] inside onClick function, it does not fire. But when I try onStart event, it fired perfectly normal.

 

Please help, thanks before.

And sorry, for my bad english.

Link to comment
Share on other sites

Sounds like an overwrite issue to me. If you create a competing tween of the same object that's tweening the same property (alpha in this case), it will overwrite the previous one by default, and if that previous one has no tweening properties left, it will also kill the tween so that its onComplete doesn't fire. That's very intentional, and was requested by many users. You have 2 options:

  1. If you want the onComplete to fire regardless, just use a delayedCall() instead. So you'd have a tween for the alpha and a delayedCall() for the function call
  2. Or set overwrite:"none" on the tween, but I generally try to avoid that because you're allowing conflicts to happen so that the alpha is getting set twice on each frame. 

Does that help?

Link to comment
Share on other sites

hey, thanks for fast response :)

 

Actually I want to load an external SWF into a MovieClip on stage and add fade-in, fade-out effect every time user click other button to load another external SWF.

 

Here are the steps:

1. User click button to load external SWF

2. After external SWF loaded & added to 'container' MovieClip, run Fade-In effect.

3. User click other button to load other external SWF

4. Fade-Out 'container' MovieClip.

5. Remove all children inside 'container' MovieClip.

6. Load new external SWF and redo to step 2.

 

That's why I can't use delayedCall(), because it is depend on user interaction & asset load time.

 

Here I attach the files, just in case :)

Thanks greensock.

swf_loader.fla.zip

01,02,03,04.zip

Link to comment
Share on other sites

Hi, 

 

I looked at your files and everything worked as expected, or atleast as it was programmed to work. 

 

As for your first issue, I could not reproduce an error where the tween's onComplete did not fire when it was expected to.

 

Its very important to realize:

 

1: the tween with the onComplete callback is inside a conditional statement that will only allow it to play if a certain number of children exist in sub_container

 

if(sub_container.numChildren > 0){

        trace("fadeOut");
        //sub_container.getChildAt(0).rotation = 45;
        TweenMax.to(sub_container.getChildAt(0), 1, {alpha:0, onComplete:clearChildren, onCompleteParams:["yaay"]});
    }

 

There are many times that you can click a button and sub_container.numChildren will 0 because you are calling removeAllChildren() at the wrong time.

 

Your logic assumes that the tween will finish before the swf is loaded. This is not good. What's happening is your swfs are loading  instantly (due to their small file size) and as soon as the alpha:0 tween completes on the existing childswf, your clearChildren function is removing the old swf AND the new swf with your removeAllChildren function. So the next time you hit a button, sub_container.numChildren is 0, no tween runs, and no callback fires.

 

What you should do is call removeAllChildren() immediately prior to adding the new swf to the stage

 


 

 

 

import flash.events.MouseEvent;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import com.greensock.TweenMax;
 
stop();
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var basePath:String = "";
var projectName:String = "";
var theSWF:String = "";
 
banners.banner1.addEventListener(MouseEvent.CLICK, onClick);
banners.banner2.addEventListener(MouseEvent.CLICK, onClick);
banners.banner3.addEventListener(MouseEvent.CLICK, onClick);
banners.banner4.addEventListener(MouseEvent.CLICK, onClick);
 
function onClick(me:MouseEvent):void{
    //trace(sub_container.numChildren);
    if(sub_container.numChildren > 0){
        trace("fadeOut");
        //sub_container.getChildAt(0).rotation = 45;
        TweenMax.to(sub_container.getChildAt(0), 1, {alpha:0});
    }
    switch(me.currentTarget.name){
        case "banner1":
            theSWF = "01.swf";
        break;
        case "banner2":
            theSWF = "02.swf";
        break;
        case "banner3":
            theSWF = "03.swf";
        break;
        case "banner4":
            theSWF = "04.swf";
        break;
    }
    var swfLoader:Loader = new Loader();
    swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadSubComplete);
    swfLoader.load(new URLRequest(basePath + projectName + theSWF));
}
function clearChildren(param):void{
    trace("param"+param);
    
    trace(sub_container.numChildren);
}
function onLoadSubComplete(ev:Event):void{
    var theLoaded:MovieClip = MovieClip(ev.target.content);
    theLoaded.x = -theLoaded.width/2;
    theLoaded.y = -theLoaded.height/2;
    sub_container.removeAllChildren();
    sub_container.addChild(theLoaded);
    //you have to set the alpha of theLoaded to 0 first in order to tween to alpha:1
    TweenMax.fromTo(theLoaded, .5, {alpha:0}, {alpha:1, immediateRender:true});
}
 
stage.addEventListener (Event.RESIZE, resizeListener);
 
function resizeListener(ev:Event):void{
    sub_container.x = stage.stageWidth/2;
    sub_container.y = stage.stageHeight/2;
    banners.x = stage.stageWidth/2 - banners.width/2;
}
 
DisplayObject.prototype.removeAllChildren = function(){    
    for(var j = this.numChildren-1; j >= 0; j--){
        this.removeChildAt(j);
    }
}

 

 

 

Also note that since you were placing the new swf on top of the old swf, you would rarely see the old swf fade out, because it was being completely covered up. WIth my code the old swf is getting removed as soon as the new swf is done loading, but the introduction of the new swf uses a nice fade-in since I added the fromTo tween.

 

 
Thanks a bunch for providing the files. They made it much easier to figure this out.
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...