# How do I "repeat" a Timeline with changing values each time?

## Recommended Posts

Hello.

I am in need of serious help with this problem I'm facing.

First off let me tell you what I want to achieve with my code.

On click of a button at the top-center of my screen, 4 fishes are to be tweened with bezier movements to simulate 'swimming' through water. They have other functions but this is the part that I need to get working.

```function tweenFish():void
{
var numY:Array = new Array;

for (var count:Number = 1; count < 5; count++)
{
numY.push(count+8);
}
numY.reverse();
trace(tweenArr, round);
for (var numX:Number = 0; numX < 4; numX++)
{
var randomStart:Number = (Math.floor(Math.random() * (460 - 140 + 1)) + 140);
_difficulty[numX].y = randomStart;
_difficulty[numX].x = -50;
if (round == 1)
{

tweenArr[numX] = TweenMax.to(_difficulty[numX], (numY[numX]/round), {bezier:{curviness:2, autoRotate:true, values:[{x:50, y:randomStart+5}, {x:150, y:randomStart-5}, {x:250, y:randomStart+5}, {x:350, y:randomStart-5}, {x:450, y:randomStart+5}, {x:550, y:randomStart-5}, {x:770, y:randomStart+5}]}, ease:Cubic.easeInOut});
}
}
}
```

This is the function I use to setup the tweens for the fishes. Each fish (set in an array called _difficulty) is given a set x value (offscreen) and a random y value so that each run they will 'swim' across the stage. This works perfectly. In fact, all of it runs perfectly...until I try to run it again.

This is my initialization which basically stops the round if the fishes make it off the stage without being clicked (intended functionality).

```var tMax:TimelineMax = new TimelineMax({onComplete:endRound});
```

And this is the function it calls.

```function endRound():void
{
GoFishing.removeEventListener(MouseEvent.CLICK, fish);
while (tweenArr.length > 0)
{
tweenArr.length = 0;
}
//			tMax.clear(); POSSIBLE CODE?

gotoAndStop("endGameResults");
scoreBox.text = "Your score is: " + points;
gameResultsBG.width = 1;
gameResultsBG.height = 1;
TweenLite.to(gameResultsBG, 1.5, {scaleX:1.1, scaleY:1.1});
TweenLite.to(gameOverText, 3, {autoAlpha:1});
TweenLite.to(playAgain, 2, {visible:true});
timerX.stop();
timerX.removeEventListener(TimerEvent.TIMER, clock);
function(e:MouseEvent):void
{
MovieClip(root).gotoAndStop(1);
round = 0;
}
);
}
```

Don't mind the commented line at the top.

Anyway, this function leads to frame 2 where it's an end-game screen and it allows you to retry. 'playAgain' would take you to frame 1 and play the tween again when the button at the top is clicked, or so I thought. This is where the fishes are frozen off screen (I expanded the window and saw), and they do not move when the function is called, BUT the timer for the timeline STILL RUNS. Know why?

The timeline takes 10 seconds to run each time at first. On the second run, 10 seconds pass and it leads me to the end-game screen. So clearly the timeline is running as I would expect it to, but the fishes aren't being moved. Is there something wrong with my code here? Do I need a different approach?

Also I just thought of this: Would disabling these fishes, or switching to another frame at any point mess up the tween functionality?

Thank you for your help.

##### Share on other sites

Hi and welcome to the GreenSock forums,

Yes, its very likely that switching to another frame is causing a problem, especially if you have code executing that references objects that don't exist on that frame. or maybe your items don't have instance names on frame 2 but they did on frame 1. Impossible for me to know by looking at a few code snippets.

There are very few times that switching frames in Flash is necessary. I would suggest instead that you place all your frame 1 assets in 1 movieclip and all frame 2 assets in another movie clip and simply toggle their visibility instead of moving the playhead.

It seems odd that you have a gotoAndStop() command followed by a lot more code. I would think moving frames would be the last thing you want to do.

clearing your TimelineMax is a good idea, but make sure you also tell the timeline to play() again once you add new tweens.

Unfortunately we can't troubleshoot your full production code, but if you can create a very simple fla that ONLY contains the functionality of a few fish swimming (in a straight line preferably) perhaps we can take a peek. The idea is that we only need to see enough code and assets to clearly replicate the problem - which is a timeline that plays again with different values.

You can zip your fla and post it here (leave out the greensock files to save file size)

thanks

##### Share on other sites

Currently my .as file is separate from the .fla file. I'll show you both, though you may have more interest in the .as file.
Ok so I have uploading size restrictions and I can't get both files below 500KB. Not sure how I'm gonna upload it here. However I'll link you to both files.

Note: I've done some edits to the code in order to try and identify the problem and it does seem that switching between frames is definitely causing the problem though I am not sure why.

AS File.zip

##### Share on other sites

I downloaded the separate  actionscript file and fla. I tried exporting and got an error about a missing xml file.

I tried looking at the actionscript but it was 500 lines. I just don't have the time to sort through that.

Again, switching frames is not good, especially if in frame 2 you remove all the fishes from the display list which it appears you re doing by having a keyframe in frame 2 that does not contain the fishes.

Also you shouldn't drive the playhead to frame2 and expect code on frame1 to continue executing properly as I see here:

```gotoAndStop("endGameResults");
scoreBox.text = "Your score is: " + points;
gameResultsBG.width = 1;
gameResultsBG.height = 1;
TweenLite.to(gameResultsBG, 1.5, {scaleX:1.1, scaleY:1.1});
TweenLite.to(gameOverText, 3, {autoAlpha:1});
TweenLite.to(playAgain, 2, {visible:true});```

Sorry, but I think you have to re-think your file setup and keep everything on frame1.

##### Share on other sites

Ok sure thing. I'm going to operate on a single frame from now on. Oh and yes I forgot to send the xml file along, but it's not very important for the problem I'm having currently.

Thanks for checking it out for me though.

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

• ### Recently Browsing   0 members

• No registered users viewing this page.
×