Jump to content
Search Community

Issue: EventListeners being ignored

Manic154 test
Moderator Tag

Recommended Posts

Hi everyone,

 

This is my first ever post to this forum. I've been getting into TweenMax and TimelineMax recently, but I seem to have hit a brick wall with my code.

I've created a menu system that animates new buttons onto the screen when required, then sends them away when not. Works at first until you try to loop through the process again. It seems to just ignore the listeners I created!

 

I have listed the code below so you can see the problem for yourself: (Note only food, home and wine buttons work)

Any help at all would be greatly appreciated!

 

 

import com.greensock.*;

import com.greensock.easing.*;

import com.greensock.TimelineMax;

import flash.events.MouseEvent;

 

 

//These are the titles for the buttons

home_b.title_box.title.text = "Home";

food_b.title_box.title.text = "Food & Wine";

tea_b.title_box.title.text = "Tea & Coffee";

gallery_b.title_box.title.text = "Gallery";

events_b.title_box.title.text = "Events";

about_b.title_box.title.text = "About Us";

lunch_b.title_box.title.text = "Lunch";

six_b.title_box.title.text = "Six-Five Special";

pullman_b.title_box.title.text = "Pullman Dining";

wine_b.title_box.title.text = "Winelist";

wine_b.title_box.title.text = "Winelist";

white_b.title_box.title.text = "White";

red_b.title_box.title.text = "Red";

rose_b.title_box.title.text = "Rose";

champ_b.title_box.title.text = "Champagne";

 

 

//These are the buttons starting places on the screen

home_b.x = 0;

home_b.y = 180;

 

food_b.x = 0;

food_b.y = 210;

 

tea_b.x = 0;

tea_b.y = 240;

 

gallery_b.x = 0;

gallery_b.y = 270;

 

events_b.x = 0;

events_b.y = 300;

 

about_b.x = 0;

about_b.y = 330;

 

lunch_b.x = -150;

lunch_b.y = 60;

 

six_b.x = -150;

six_b.y = 90;

 

pullman_b.x = -150;

pullman_b.y = 120;

 

wine_b.x = -150;

wine_b.y = 150;

 

white_b.x = -150;

white_b.y = 180;

 

red_b.x = -150;

red_b.y = 210;

 

rose_b.x = -150;

rose_b.y = 240;

 

champ_b.x = -150;

champ_b.y = 270;

 

 

//Buttons are currently invisible

home_b.alpha = 0;

food_b.alpha = 0;

tea_b.alpha = 0;

gallery_b.alpha = 0;

events_b.alpha = 0;

about_b.alpha = 0;

lunch_b.alpha = 0;

six_b.alpha = 0;

pullman_b.alpha = 0;

wine_b.alpha = 0;

white_b.alpha = 0;

red_b.alpha = 0;

rose_b.alpha = 0;

champ_b.alpha = 0;

 

 

//Animate buttons onto screen

var ShowMenu:TimelineMax = new TimelineMax({onComplete:ListenOut});

ShowMenu.append( TweenMax.to(home_b, 0.3, {alpha:1, y:0, ease:Sine.easeIn}) );

ShowMenu.append( TweenMax.to(food_b, 0.3, {alpha:1, y:30, ease:Sine.easeIn, delay:-0.2}) );

ShowMenu.append( TweenMax.to(tea_b, 0.3, {alpha:1, y:60, ease:Sine.easeIn, delay:-0.2}) );

ShowMenu.append( TweenMax.to(gallery_b, 0.3, {alpha:1, y:90, ease:Sine.easeIn, delay:-0.2}) );

ShowMenu.append( TweenMax.to(events_b, 0.3, {alpha:1, y:120, ease:Sine.easeIn, delay:-0.2}) );

ShowMenu.append( TweenMax.to(about_b, 0.3, {alpha:1, y:150, ease:Sine.easeIn, delay:-0.2}) );

 

 

function ListenOut():void{

food_b.addEventListener(MouseEvent.MOUSE_UP, openFood);

}

 

 

 

//If food button is pressed, do this:

function openFood(event:MouseEvent):void{

showFood.play();

}

 

 

//Animate buttons down, then look for food list

var showFood:TimelineMax = new TimelineMax({paused:true, onComplete:ListenOut2});

 

var food:Array = new Array(lunch_b, six_b, pullman_b, wine_B);

 

 

//Food list then wine list

showFood.append( TweenMax.to(about_b, 0.3, {alpha:1, y:270, ease:Sine.easeIn}) );

showFood.append( TweenMax.to(events_b, 0.3, {alpha:1, y:240, ease:Sine.easeIn, delay:-0.2}) );

showFood.append( TweenMax.to(gallery_b, 0.3, {alpha:1, y:210, ease:Sine.easeIn, delay:-0.2}) );

showFood.append( TweenMax.to(tea_b, 0.3, {alpha:1, y:180, ease:Sine.easeIn, delay:-0.2}) );

showFood.appendMultiple( TweenMax.allTo(food, 0.3, {alpha:1, x:50, ease:Sine.easeOut}, 0.1) );

 

 

function ListenOut2():void{

 

//Home button now active

home_b.addEventListener(MouseEvent.MOUSE_UP, closeFood);

 

//Wine button now active

wine_b.addEventListener(MouseEvent.MOUSE_UP, openWine);

}

 

 

function closeFood(event:MouseEvent):void{

//Home button no longer takes us home

home_b.removeEventListener(MouseEvent.MOUSE_UP, closeFood);

 

//Wine button no longer active

wine_b.removeEventListener(MouseEvent.MOUSE_UP, openWine);

 

showFood.reverse();

}

 

 

function openWine(event:MouseEvent):void{

 

showWine.play();

}

 

var showWine:TimelineMax = new TimelineMax({paused:true, onComplete:ListenOut3});

 

var wines:Array = new Array(white_b, red_b, rose_b, champ_B);

 

//These buttons slide down

showWine.append( TweenMax.to(about_b, 0.3, {alpha:1, y:390, ease:Sine.easeIn}) );

showWine.append( TweenMax.to(events_b, 0.3, {alpha:1, y:360, ease:Sine.easeIn, delay:-0.2}) );

showWine.append( TweenMax.to(gallery_b, 0.3, {alpha:1, y:330, ease:Sine.easeIn, delay:-0.2}) );

showWine.append( TweenMax.to(tea_b, 0.3, {alpha:1, y:300, ease:Sine.easeIn, delay:-0.2}) );

showWine.appendMultiple( TweenMax.allTo(wines, 0.3, {alpha:1, x:100, ease:Sine.easeOut}, 0.1) );

 

function ListenOut3():void{

 

//Home button no longer takes you home from food

home_b.removeEventListener(MouseEvent.MOUSE_UP, closeFood);

home_b.addEventListener(MouseEvent.MOUSE_UP, closeWine);

 

//wine button no longer active

wine_b.removeEventListener(MouseEvent.MOUSE_UP, openWine);

}

 

 

function closeWine(event:MouseEvent):void{

 

closeEverything.play();

}

 

 

var closeEverything:TimelineMax = new TimelineMax({paused:true, onComplete:ListenOut});

 

var FoodWineArray:Array = new Array(champ_b, rose_b, red_b, white_b, wine_b, pullman_b, six_b, lunch_B);

 

closeEverything.appendMultiple(TweenMax.allTo(FoodWineArray, 0.3, {alpha:0, x:-150, ease:Sine.easeIn}, 0.1) );

closeEverything.append( TweenMax.to(tea_b, 0.3, {alpha:1, y:60, ease:Sine.easeIn}) );

closeEverything.append( TweenMax.to(gallery_b, 0.3, {alpha:1, y:90, ease:Sine.easeIn, delay:-0.2}) );

closeEverything.append( TweenMax.to(events_b, 0.3, {alpha:1, y:120, ease:Sine.easeIn, delay:-0.2}) );

closeEverything.append( TweenMax.to(about_b, 0.3, {alpha:1, y:150, ease:Sine.easeIn, delay:-0.

Link to comment
Share on other sites

that's a lot of code to digest.

 

are you talking about the onComplete listeners not working or your buttons?

 

have you tried putting traces in your listener functions to verify that they are being fired?

 

also, if your timeline's simply aren't playing when you click buttons you could try restart().

Link to comment
Share on other sites

Hi Carl,

 

Sorry for the long code...I'm not exactly sure where the error is!

 

In answer to your question, yes...I have run a trace, which is why i'm so puzzled. The code definitely directs to the correct places when the 'onComplete' is fired. It simply seems to ignore the code in it.

 

Specifically, I'm talking about the bit at the bottom of the code there. The last 'onComplete' function. It seems to ignore the event listener in there. All other onComplete's seem fine however.

 

Wish I could just post up the .fla here. Far easier to see for yourself than to try to explain it! lol

 

If you are interested in checking it out, all you need to do is create a movie clip that contains a box and a text field. Then simply copy it a few times and give them the instance names at the top of the code.

 

I've been staring at this thing for days trying to work this out! lol

Link to comment
Share on other sites

*UPDATE*

 

I think i've narrowed down the problem to here (very near end of code):

 

var closeEverything:TimelineMax = new TimelineMax({paused:true, onComplete:ListenOut});

 

 

The listener contained within 'ListenOut' is the one being ignored.

 

 

I find this really odd as the code works at the beginning when you first run it.

Link to comment
Share on other sites

Ah, ok thanks.

 

Here it is if you're interested in trying it out.

 

Just to take you through the steps should you try it.

 

 

1. Menu appears (great)

2. Click on 'Food and Wine' and a new menu appears (great)

 

Here you can do one of two things:

 

1. Click on either 'Home' or 'Food and Wine' again to close the options

or...

2. Click the 'Winelist' to see another new menu (great)

 

 

This is where the trouble occurs...

 

If you open 'Food and Wine', then 'Winelist' and then 'Home' it returns home and all seems well.....until you click on 'Food and Wine', but nothing happens...

Link to comment
Share on other sites

ok, the file helped and so did your very clear explanation.

 

I found some of the problems.

 

as I noted previously, using restart() instead of play() will solve some of the issues, here is why:

 

in your openFood do this:

 

function openFood(event:MouseEvent):void{

trace("open food");
trace(food.currentProgress);
food.play();
food_b.removeEventListener(MouseEvent.MOUSE_UP, openFood);
}

 

 

note the currentProgress.

 

go through these steps

 

click on food

click on winelist

click on home

click on food

 

notice the food.currentProgress = 1?

 

that means the timeline is finished. calling play() does not start it from the beginning so you need restart()

 

change your open food to:

 

function openFood(event:MouseEvent):void{

trace("open food");
trace(food.currentProgress);
food.restart();
food_b.removeEventListener(MouseEvent.MOUSE_UP, openFood);
}

 

now test

 

click on food

click on winelist

click on home

click on food

 

it works!

 

the problem is your closeWineFood timeline is NOT reversing wine or food. they stay "completed" so when you go to play them again (if not using restart) they don't work.

 

you will see the same issue exists with openWine

 

add the follwing traces to openWine:

 

function openWine(event:MouseEvent):void{
trace("openWine ");
trace("wine.currentProgress = " + wine.currentProgress);
food.stop();
wine.play();
wine_b.removeEventListener(MouseEvent.MOUSE_UP, openWine);
food_b.removeEventListener(MouseEvent.MOUSE_UP, closeFood);
home_b.removeEventListener(MouseEvent.MOUSE_UP, closeFood);
}

 

 

click on food

click on winelist

click on home

click on food

click on winelist

 

winelist won't open because its currentProgress is 1.

 

change wine.play() to wine.restart() in openWine and it will work.

 

---------------

Link to comment
Share on other sites

Hi Carl,

 

Thank you so much. I went through all the steps you suggested so as to see what was going on. On my search around the web looking to solve the problem I was beginning to suspect it was something to do with restart() being required, but wasn't sure.

 

Also, I realised I had to change closeWineFood.play(); to closeWineFood.restart(); in the 'closeAll' function, or the same problem would occur should you go though the button sequence a second time.

 

Again, thank you so much. Hope I can return the favour sometime, though maybe not with TweenMax or TimelineMax as you clearly are more experienced with it than I (Having used it now for only 4 days! lol). However, sometimes a fresh pair of eyes makes all the difference. If needed, please drop a comment in here.

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