Skip navigation
Angyl
Currently Being Moderated

Random moviclip on stage

Jul 15, 2012 3:24 PM

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.

 
Replies
  • Currently Being Moderated
    Jul 15, 2012 4:01 PM   in reply to Angyl

    Hello

    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++) {
              attachRandomMovie(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.
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 15, 2012 3:59 PM   in reply to Angyl

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points