Timer Help

Hello, I’m hoping someone can help me. I have an mp3 playing. What I would like is to have a movieclip that is located in the library appear, complete it’s animation then get removed. The movie clip must complete this proccess 4 times, randomly during the length of the song but never at the same time and not in the last 5 seconds of the song. BTW I can’t do this manually by just firing it off a random amount of times because this code will be used with lots of different mp3’s, so I need to use the dynamic data I get by retrieving the song length.

I have the code working to retrieve the length of the song, and I have a variable that stores the length minus 5 seconds from the end, so I bascially have the length of time I wish to work with. My problem is making the movie clips appear 4 times randomly throughout that length of time. The code is below (what I have so far), the movieclip is stored in the library as circle_mc.

The timer I’m using at the beginning of the code (to retrieve the length of the song) is set to run after 2 seconds this is because there is a delay in the music starting and if it did it instantly the number retrieved obviously would be 0, so I delay it by 2 secs till I know the song is playing.

At the moment I’m just trying to get it to fire twice, at the moment it fires once or not at all, if you know an easier way or make my way work please help


import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import caurina.transitions.Tweener;

var req:URLRequest=new URLRequest("song.mp3");
var sound:Sound=new Sound(req);
var ba:ByteArray=new ByteArray();
var circle:MovieClip = new circle_mc();


sound.play();

var circleOnStage:Boolean;
circleOnStage = false;


var myTimer:Timer=new Timer(2000,1);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
function timerListener(e:TimerEvent):void {
var Milliseconds:Number = (sound.bytesTotal / (sound.bytesLoaded / sound.length));
    
var Minutes:uint = Math.floor(Milliseconds/60000);
var Seconds:uint = (Milliseconds%60000);
var SecondsTens:uint = Math.floor(Seconds/10000);

Seconds = Math.ceil(Seconds%10000)
Seconds /= 1000;

var soundLength:uint = (sound.length) - 5000;

            if (circleOnStage==false) {
            var circleTimer1:Timer=new Timer(Math.random()* soundLength, 1);
            circleTimer1.addEventListener(TimerEvent.TIMER, circleTimer1Listener);
            circleTimer1.start();
            function circleTimer1Listener(e:TimerEvent):void {
                addChild(circle);
               circle.x=Math.random()*800;
                circle.y=833;
                circle.alpha=1;
                Tweener.addTween(circle, {x:Math.random()*800, y:150, alpha:0, time:4, transition:"linear", onComplete:func});

                function func() {
                    
                    removeChild(circle);
                    circleOnStage=false;
                }}

                

            if (circleOnStage==false) {
            
            var circleTimer2:Timer=new Timer(Math.random()* soundLength, 1);
            circleTimer2.addEventListener(TimerEvent.TIMER, circleTimer2Listener);
            circleTimer2.start();
            function circleTimer2Listener(e:TimerEvent):void {
                addChild(circle);
                circle.x=Math.random()*800;
                circle.y=833;
                circle.alpha=1;
                Tweener.addTween(circle, {x:Math.random()*800, y:150, alpha:0, time:4, transition:"linear", onComplete:func});

                function func() {
                    
                    removeChild(circle);
                    circleOnStage=false;
            
        
        }}}}}