PDA

View Full Version : Event listener to check that function complete



andrewF
March 3rd, 2010, 01:46 PM
I am trying to create an event listener to check that a function has completed before the next action is run. In simple terms I have a module that is opened and closed via two functions... module_open() and module_close().

Each function has tween elements that must complete before the next element is processed... i.e the next function, etc.

This is the code I have ( have attempted to create an event listener but I seem doing something wrong. I am not even sure that it is possible to create an event listener that checks for a function finishing before calling the next function.



function onAbout(event:Event):void {

module_close()
module_close.addEventListener(Event.COMPLETE, onClose_Complete);

function onClose_Complete():void{

logo_block.gotoAndPlay("about");
title_text.gotoAndStop("about");

if (empty_mc_main.numChildren >0) {
empty_mc_main.removeChildAt(0);
}

var myMovieClip:MovieClip = new about_page_mc();
empty_mc_main.addChild(myMovieClip);

living_btn.visible = false;
lifestyle_btn.visible = false;
elements_btn.visible = false;
colour_btn.visible = false;

}

}I expect there is a standard way of doing this.

Any help much appreciated.

Thx

BoppreH
March 3rd, 2010, 03:18 PM
Code is run sequentially. If the next part of the code is already running, all code preceding it has been executed.

Could you specify what's inside the "module_close" function?

And as a side note, do not nest functions needlessly like this.

andrewF
March 3rd, 2010, 06:16 PM
This is the code for the 'module_close' function:




function module_close():void{

var closeNavTween: Tween = new Tween(nav_but_Mc, "alpha", None.easeOut, nav_but_Mc.alpha, 0, 1, true);

closeNavTween.addEventListener(TweenEvent.MOTION_F INISH, onFinish_nav);

function onFinish_nav(e:TweenEvent):void {

var closingYTween1: Tween = new Tween(opening_Mc, "y", Regular.easeOut, opening_Mc.y, 210, 2, true);
var closingYTween2: Tween = new Tween(opening_Mc, "height", Regular.easeOut, opening_Mc.height, 1, 2, true);

closingYTween2.addEventListener(TweenEvent.MOTION_ FINISH, onFinish_heightClose);

function onFinish_heightClose(e:TweenEvent):void {

var closeXTween1: Tween = new Tween(opening_Mc, "x", Regular.easeOut, opening_Mc.x, 960, 1, true);
var closeXTween2: Tween = new Tween(opening_Mc, "width", Regular.easeOut, opening_Mc.width, 1, 1, true);

closeXTween2.addEventListener(TweenEvent.MOTION_FI NISH, onFinish_close);

function onFinish_close() : void {
search_input_txt.visible = true;
nav_but_Mc.select_box.x = 386;
go_btn.visible = true;
search_txt.visible = true;
}

}
}
}

The tweens in this function need to complete before the 'about_page_mc' movie clip loads.

Currently this is not the case and the new movie clip loads over the tween.... so the 'module_close' function is not completing before the next action takes place.

I thought that code does action sequentially but this seems to be defying that rule.

as always... all help welcome.

andrewF
March 3rd, 2010, 06:45 PM
And as a side note, do not nest functions needlessly like this.


What do you mean by this...? Is it wrong to call a function within a function... I was trying to simplify the code by creating functions for code that is repeated a lot.

Thxs

A.

_kp
March 3rd, 2010, 07:21 PM
What do you mean by this...? Is it wrong to call a function within a function... I was trying to simplify the code by creating functions for code that is repeated a lot.

Thxs

A.

Using functions is good, it's just that they shouldn't be declared inside other functions.

For example you second code looks like this:


function module_close():void{
//...
function onFinish_nav(e:TweenEvent):void {
//...
function onFinish_heightClose(e:TweenEvent):void {
//...
}
}
}

This is better:


function module_close():void{
//...
}

function onFinish_nav(e:TweenEvent):void {
//...
}

function onFinish_heightClose(e:TweenEvent):void {
//...
}


This way you are be able to access those function from outside of module_close and could reuse them if you wanted to.

theCodeBot
March 3rd, 2010, 07:21 PM
What do you mean by this...? Is it wrong to call a function within a function... I was trying to simplify the code by creating functions for code that is repeated a lot.

Thxs

A.

He's not saying it's wrong to call a function within a function, but rather that it's a bad idea to define (create) a function within a function. All you need to do is move the function definition outside of the contents of the other function. Defining a function takes up a lot of CPU cycles, especially when you're defining and calling it on every single iteration of a loop, like you're doing. That, and it gets a lot harder to debug things when you do that.

You have this now:


function someFunction(argument)
{
function someOtherFunction(argument2)
{
function someThirdFunction(argument3) {...}
someThirdFunction();
}
someOtherFunction();
}


What you should have is this:


function someFunction(argument)
{
someOtherFunction(argument2);
someThirdFunction(argument3);
}
function someOtherFunction(argument2)
{
//...
}
function someThirdFunction(argument3)
{
//...
}

BoppreH
March 3rd, 2010, 07:26 PM
This is the code for the 'module_close' function:




function module_close():void{

var closeNavTween: Tween = new Tween(nav_but_Mc, "alpha", None.easeOut, nav_but_Mc.alpha, 0, 1, true);

closeNavTween.addEventListener(TweenEvent.MOTION_F INISH, onFinish_nav);

function onFinish_nav(e:TweenEvent):void {

var closingYTween1: Tween = new Tween(opening_Mc, "y", Regular.easeOut, opening_Mc.y, 210, 2, true);
var closingYTween2: Tween = new Tween(opening_Mc, "height", Regular.easeOut, opening_Mc.height, 1, 2, true);

closingYTween2.addEventListener(TweenEvent.MOTION_ FINISH, onFinish_heightClose);

function onFinish_heightClose(e:TweenEvent):void {

var closeXTween1: Tween = new Tween(opening_Mc, "x", Regular.easeOut, opening_Mc.x, 960, 1, true);
var closeXTween2: Tween = new Tween(opening_Mc, "width", Regular.easeOut, opening_Mc.width, 1, 1, true);

closeXTween2.addEventListener(TweenEvent.MOTION_FI NISH, onFinish_close);

function onFinish_close() : void {
search_input_txt.visible = true;
nav_but_Mc.select_box.x = 386;
go_btn.visible = true;
search_txt.visible = true;
}

}
}
}

The tweens in this function need to complete before the 'about_page_mc' movie clip loads.

Currently this is not the case and the new movie clip loads over the tween.... so the 'module_close' function is not completing before the next action takes place.

I thought that code does action sequentially but this seems to be defying that rule.

as always... all help welcome.
It is doing the code sequentially. Problem is, the tween part is basically made of "start" statements and don't actually cover the whole tween movement, thus that is not included as part of the function and not run sequentially.

You must attach the event listener to the Tween in order to check when it's over and call the next function.

And consider using TweenLite (http://www.greensock.com/tweenlite/) instead of the built-in tween classes. It has a very neat property called "onComplete" that calls functions with arbitrary arguments when the tween is finished.

andrewF
March 11th, 2010, 12:01 PM
After a bit of digging I managed to sort myself out... Tweenlite was a master stroke... made things much easier.

All working... now on to the next nutty problem

Thanks for everyones help

Andrew