2 Replies Latest reply on Jul 15, 2012 3:59 PM by Ned Murphy

    Random moviclip on stage


      Hi,  I want to help my daughter with her musical instrument practice and it occurred to me to create a flash movie that will randomly place notes on a scrolling staff for her to play.


      I can figure out how to do just about everything except how to get flash to randomly grab one of (say 5) the movieclip notes in the library and place it randomly in one of NINE y positions on the stage (the staff).  I plan to have this all in a movieclip that will scroll from right to left.


      So basically I need help with 2 code pieces:


      1.  Grab a random movieclip from the library

      2.  Place that movieclip randomly on one of 9 y coordinates on the stage.

        • 1. Re: Random moviclip on stage
          prodigalmaster Level 2


          Make the 5 movieclips (you can add more if you want) and make sure they have a linkage name so they can be accessed from the library.



          var rndMovie:Array = new Array("mc1", "mc2", "mc3", "mc4", "mc5");//change these to fit the name of the notes movieclips
          var staffPositions:Array = new Array(10, 50, 90, 130, 170, 210, 250, 290, 330);//these are the y positions
          var notesAttached:Number = 1;//how many mcs to randomly attach (if more than 1 then they may overlap (make a new array to store positions)).
          for (i=0; i<notesAttached; i++) {
          function attachRandomMovie(depth) {
                    var depth:Number;
                    mc = attachMovie(rndMovie[Math.floor(Math.random()*rndMovie.length)], "mc", depth);//attach a random movieclip using the rndMovie array from the library
                    mc._x = Stage.width/2;//change this to change the x position;
                    mc._y = staffPositions[Math.floor(Math.random()*staffPositions.length)];//moves the mc to a random y position from the positions in the staffPositions array.
          • 2. Re: Random moviclip on stage
            Ned Murphy Adobe Community Professional & MVP

            You use the attachMovie() method to bring in objects from the library.  The objects need to hae a linkage ID assigned to them in the library to support that.


            To randomly pick things, it is usually easiest to place them in an array and then use the Math.random() function to generate a random index which you use to pick one of the array elements.