6 Replies Latest reply on Aug 14, 2010 12:14 PM by kglad

    Play animation (movie clip) randomly as3

    spinkowalski

      Hello,

       

      Could anyone out there tell me what as3 code i should be using to make this happen.

       

      I want the animation (shooting star) to play at random times.

       

      Many thanks indeed.

       

      Damien

        • 1. Re: Play animation (movie clip) randomly as3
          kglad Adobe Community Professional & MVP

          what's the instance name of your shooting star movieclip?  does it have a stop() on its timeline?  if yes, which frame?

          • 2. Re: Play animation (movie clip) randomly as3
            Ned Murphy Adobe Community Professional & MVP

            You can use the Timer class and have the delay property reassigned as a randomly calculated value each time the Timer fires.

            • 3. Re: Play animation (movie clip) randomly as3
              spinkowalski Level 1

              The movie clip is called 'starmove'

               

              Its placed on the first frame, no 'stop' in the actions, it just loops in the same position and time.

               

              Preferably I would like various instances in different positions to randomly play, to give the effect of real shooting stars.

               

              Any help gratefully received

               

              Damien

              • 4. Re: Play animation (movie clip) randomly as3
                kglad Adobe Community Professional & MVP

                on the last frame of your movieclip attach:


                stop();

                this.dispatchEvent(new Event("finished"));

                 

                right click your movieclip in the library, click linkage, tick export for actionscript and assign a class (eg, Starmove).

                 

                you can then use:

                 

                var minTime:uint = 1000;

                var maxTime:uint = 3000;


                var timer:Timer=new Timer(timeF(),0);

                timer.addEventListener(TimerEvent.TIMER,starF);

                timer.start();


                function starF(e:TimerEvent){

                var star:Starmove=new Starmove();

                addChild(star);

                star.x = Math.floor(Math.random()(stage.stageWidth-star.width));  // these two will probably need adjustment

                star.y = Math.floor(Math.random()(stage.stageHeight-star.height)); // ditto

                star.addEventListener("finished",removeStarF);

                timer.delay = timeF();

                }


                function removeStarF(e:Event){

                removeChild(e.currentTarget);

                e.currentTarget.removeEventListener("finished",removeStarF);

                e.currentTarget=null;

                }


                function timeF():uint{

                return minTime+Math.floor(Math.random()*(maxTime-minTime));

                }

                • 5. Re: Play animation (movie clip) randomly as3
                  spinkowalski Level 1

                  Hey Kglad, thank you.. movie plays the same as without your code (no change) in a loop, and there are two compiler errors:-

                   

                  Scene 1, Layer 'Layer 2', Frame 1, Line 24 1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.

                  Scene 1, Layer 'Layer 2', Frame 1, Line 26 1059: Property is read-only.
                  Any ideas?

                  • 6. Re: Play animation (movie clip) randomly as3
                    kglad Adobe Community Professional & MVP

                    use:

                     


                    var minTime:uint = 1000;

                    var maxTime:uint = 3000;


                    var timer:Timer=new Timer(timeF(),0);

                    timer.addEventListener(TimerEvent.TIMER,starF);

                    timer.start();


                    function starF(e:TimerEvent){

                    var star:Starmove=new Starmove();

                    addChild(star);

                    star.x = Math.floor(Math.random()(stage.stageWidth-star.width));  // these two will probably need adjustment

                    star.y = Math.floor(Math.random()(stage.stageHeight-star.height)); // ditto

                    star.addEventListener("finished",removeStarF);

                    timer.delay = timeF();

                    }


                    function removeStarF(e:Event){

                    removeChild(MovieClip(e.currentTarget));

                    e.currentTarget.removeEventListener("finished",removeStarF);

                    e.currentTarget=null;

                    }


                    function timeF():uint{

                    return minTime+Math.floor(Math.random()*(maxTime-minTime));

                    }