1
votes

I am new to action script and i am having a few problems.

I have created a movie in Flash CC with 8 scenes. Scene 6 has an action script that randomly places birds which fly across the screen. Scene 7 has an almost identical script which does the same thing only slower (using a different symbol) and with a couple of variables changed.

The first problem surfaced when testing the movie with the action script only placed in scene 6, it simply did not end at the end of scene 6 and proceeded to play through scene 7 and scene 8.

When adding the script to scene 7, it breaks completely. Testing the individual scenes work fine but when testing the whole movie nothing happens and I get errors.

Here is the action script I am using:

Scene 6

import flash.utils.Timer;

var timer: Timer = new Timer(100, 45);
timer.addEventListener(TimerEvent.TIMER, timerF);
timer.start();

function timerF(event: TimerEvent): void {
    var mcClip: Bird = new Bird();
    var yVal:Number = (Math.ceil(Math.random()*200));
    mcClip.x = -20;
    mcClip.y = yVal;
    addChildAt(mcClip,10);
}

Scene 7

import flash.utils.Timer;

var timer: Timer = new Timer(100, 40);
timer.addEventListener(TimerEvent.TIMER, timerF);
timer.start();

function timerF(event: TimerEvent): void {
    var mcClip: Bird2 = new Bird2();
    var yVal:Number = (Math.ceil(Math.random()*400));
    mcClip.x = 0;
    mcClip.y = yVal;
    addChildAt(mcClip,3);
}   

These are the errors I get:

Scene 7 (Birds), Layer 'Bird Action Script', Frame 1114, Line 3, Column 5
1151: A conflict exists with definition timer in namespace internal.

and

Scene 7 (Birds), Layer 'Bird Action Script', Frame 1114, Line 7, Column 10
1021: Duplicate function definition.

I need to stop the script at the end of each scene, but I don't know how. I have searched Google and cannot find anything helpful.

Any help would be greatly appreciated.

Many Thanks

2

2 Answers

0
votes

need to stop the script at the end of each scene

Simply place stop(); in the last frame of the scene. Select last frame of the scene, hotkey F9 will open window with actions.

Action window

0
votes

Before scene 6 finishes you need to remove the Timer event listener, stop the timer and maybe even nullify it, ie:

timer.removeEventListener(TimerEvent.TIMER, timerF);
timer.stop();
timer = null;

If you're using the timeline, the error issues might be if the frame that 'contains' the actionscript for scene 6 might be carrying on into scene 7. Placing an empty keyframe on that layer before the end of the scene might help.

EDIT - in response to new comments below:

Before the line defining your timer, add:

var _birds:Array = new Array();

Then, in your TimerEvent handler:

function timerF(event: TimerEvent): void {
    _birds.push(new Bird());
    var yVal:Number = (Math.ceil(Math.random()*200));
    _birds[_birds.length-1].x = -20;
    _birds[_birds.length-1].y = yVal;
    addChildAt(_birds[_birds.length-1], 10);
}

Then, at the end of your scene:

stop();
timer.removeEventListener(TimerEvent.TIMER, timerF);
timer.stop();
timer = null;
for (var loop:int=0;loop<_birds.length;loop++) {
    removeChild(_birds[loop]);    
}
_birds.length = 0;
// additional code here to go to next scene?

Obviously the timer variable names should change to match the names you're now using in each scene.