A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: Need Random order for movieclips presentation

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    34

    Need Random order for movieclips presentation

    Hi guys,

    I am working on a website and I have to make a flash banner under the header. In this flash banner, I have 9 different movie clips. Each clip is in fact a picture and a text animated going with it. But everytime someone go to another page in the website, the flash will restart and there's a good chance they may never see the last movieclips. But I think there's a way to call the movie clips in a random order. This way we wouldn't see always the same first mc. I don't mind working in AS2 or AS3 for this. If you give me an answer, please put as much details as possible since I am more a designer than a flash developer.

    Thank you guys !!!!
    thevibe

  2. #2
    Member
    Join Date
    Jul 2009
    Posts
    56
    well, what I would do is export each MovieClip to actionscript ( meaning, giving it an identifier ). create an instance of each one, put them all in an Array, Shuffle the Array, then just add the first one in the Array to the stage.

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    34
    Since I am such a newbie with flash, could you please write me the code for the array. More details the better! Thanks in advance.

  4. #4
    Member
    Join Date
    Jul 2009
    Posts
    56
    I cannot guarantee itll work 100%, but I think it would be something like
    PHP Code:
    // instance the MovieClips ( so theMC1 is the name of the identifier you exported to flash ) //

    mc1:theMC1 = new theMC1();
    mc2:theMC2 = new theMC2();

    // define Arrays. will need 2 arrays so we can shuffle.
    theArray:Array = new Array();
    shuffledArray:Array = new Array();

    //put the MovieClips in the array
    theArray.push(mc1mc2);

    // shuffle
    while ( theArray.length() > ) {
    var 
    whichMC:Number Math.floor(Math.random()*theArray.length());
    shuffledArray.push(theArray[whichMC]);
    theArray.splice(whichMC1);
     }

    //add to stage 
    addChild(shuffledArray[0]); 
    If you want, you can use Math.floor and Math.random to then add a random MovieClip from the shuffled array.
    I hope it works for you.

  5. #5
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Are you talking about like banner ads on websites? Cause those are like:

    Banner #1, Banner #2, Banner #3
    Person loads that website, one of the banners will display
    If Banner #1 shows up, it will always be Banner #1 unless the person refreshes the page

    If that's what you're going for then the code is simpler than the above. Sorry gamingPC I'm not trying to be mean or anything.

    PHP Code:
    var mc1:MC1 = new MC1 ();
    var 
    mc2:MC2 = new MC2 ();
    var 
    mc3:MC3 = new MC3 ();

    //Initialize the array with all the movieclips in it
    var mcarray:Array = new Array (mc1mc2mc3);

    //Pick a random number
    var randomnum:Number Math.floor (Math.random () * mcarray.length);

    //Add the movieclip with that random number in the array to the stage
    stage.addChild (mcarray [randomnum]); 
    This should do the trick, except there is still a chance that the same banner from the last page will play because it's equal chance for all banners.

    The code by gamingPC still works but it's better for say you want to play every single banner one after the other in random order, the same idea as shuffling a list of songs to play one after the other.

  6. #6
    Member
    Join Date
    Jul 2009
    Posts
    56

    fasfas

    Quote Originally Posted by 5Five555 View Post
    Are you talking about like banner ads on websites? Cause those are like:

    Banner #1, Banner #2, Banner #3
    Person loads that website, one of the banners will display
    If Banner #1 shows up, it will always be Banner #1 unless the person refreshes the page

    If that's what you're going for then the code is simpler than the above. Sorry gamingPC I'm not trying to be mean or anything.

    PHP Code:
    var mc1:MC1 = new MC1 ();
    var 
    mc2:MC2 = new MC2 ();
    var 
    mc3:MC3 = new MC3 ();

    //Initialize the array with all the movieclips in it
    var mcarray:Array = new Array (mc1mc2mc3);

    //Pick a random number
    var randomnum:Number Math.floor (Math.random () * mcarray.length);

    //Add the movieclip with that random number in the array to the stage
    stage.addChild (mcarray [randomnum]); 
    This should do the trick, except there is still a chance that the same banner from the last page will play because it's equal chance for all banners.

    The code by gamingPC still works but it's better for say you want to play every single banner one after the other in random order, the same idea as shuffling a list of songs to play one after the other.
    hmm, seams your right. I took it to far

  7. #7
    Member
    Join Date
    Jul 2009
    Posts
    34
    In fact, here's what I need with more details... sorry if I was not clear enough. So I have 9 mc. Each mc have a word and an image. When people arrive to the website, I want them to have a random choice of mc. So let's say number 4 appear. Once the mc have finished (fade in - fade out), I want another mc (random again, lets say number 8) to appear. So the 9 could appear if they stay longer enough on a page. If they go to another page the process should restart or go on...

    I don't know if you could send me a file as example of that because I tried both your codes and I am ashamed to say that neither have worked for me... I am not that good in flash.

    Thanks for taking the time guys !!!!!!

  8. #8
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    If that's the case then you need gamingPC's code.

    You should also attach your FLA file here so we can see how you made the movieclips, the names, the components, etc. It'll make it easier to help.

  9. #9
    Member
    Join Date
    Jul 2009
    Posts
    34
    I have not done the real flv yet. I was waiting to have the code first. Here what I have done this far with simple mc. I tried it different ways and cannot make it work. Thanks for your help guys!!!
    Attached Files Attached Files

  10. #10
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Well the first problem was that your file was trying to run Actionscript 2 while the code we gave is Actionscript 3.

    Second was that your movieclip linkage was different from gamingPC's. Yours was mc1, mc2, and mc3, while his was theMC1, theMC2, theMC3.

    Well this should do what you want. I added animations to the 3 movieclips so you can see them play one after the other.
    Attached Files Attached Files

  11. #11
    Member
    Join Date
    Jul 2009
    Posts
    34
    Thanks 5Five555... it's working and I understand how to put more of them. But I still have 1 last issues. I would need it to loop... for now it's stopping after all the mc have passed. I need it to continue endlessly...

    Thanks in advance!!!!

  12. #12
    Member
    Join Date
    Jul 2009
    Posts
    56
    Do you want it to loop in the same order ( like the first Randomization )
    or should it do a new randomization ?

  13. #13
    Member
    Join Date
    Jul 2009
    Posts
    34
    The easiest would be fine... thanks!!!

  14. #14
    Member
    Join Date
    Jul 2009
    Posts
    56
    Well uh, im having some problems with Flash right now.
    If you could just paste your current code here please

  15. #15
    Member
    Join Date
    Jul 2009
    Posts
    34
    Code:
    import flash.events.Event;//Import this event so you can use Event.ENTER_FRAME
    
    var mymc1:mc1 = new mc1 ();
    var mymc2:mc2 = new mc2 ();
    var mymc3:mc3 = new mc3 ();
    var mymc4:mc4 = new mc4 ();
    
    var theArray:Array = new Array (mymc1, mymc2, mymc3, mymc4);//Array of all the movieclips
    var shuffledArray:Array = new Array ();//Soon to be array of shuffled movieclips
    
    //Make the shuffled array
    while (theArray.length > 0) {
    	var whichMC:Number = Math.floor (Math.random () * theArray.length);
    	shuffledArray.push (theArray [whichMC]);
    	theArray.splice (whichMC, 1);
    }
    
    //Add the first movieclip from the array onto the stage
    this.addChild (shuffledArray [0]);
    
    shuffledArray.splice (0, 1);//Remove the first movieclip from the array so it cannot be played again
                                //This doesn't remove the movieclip from the stage, just from the 
    							//possible choices of movieclips to add
    
    //Add the Event.ENTER_FRAME event listener that runs every frame
    //24 fps means this event runs 24 times per second
    this.addEventListener (Event.ENTER_FRAME, onStageFrame, false, 0, true);
    
    //What to do every time the event runs
    function onStageFrame (e:Event) {
    	if (this.numChildren > 0) {//If there's a movieclip on the stage
    		var playingmc:* = this.getChildAt (0);//"Retrieve" it pretty much
    	
    		//If the movieclip has reached the end of its animation
    		if (playingmc.currentFrame == playingmc.totalFrames) {
    			this.removeChild (playingmc);//Remove it from the stage
    			
    			//If there is still at least one possible choice of movieclips to add
    			if (shuffledArray.length > 0) {
    				this.addChild (shuffledArray [0]);//Add the movieclip
    				shuffledArray.splice (0, 1);//Remove that movieclip from possible choices
    			}
    		}
    	}
    }

  16. #16
    Member
    Join Date
    Jul 2009
    Posts
    56

    afsfas

    PHP Code:
    import flash.events.Event;//Import this event so you can use Event.ENTER_FRAME

    var mymc1:mc1 = new mc1 ();
    var 
    mymc2:mc2 = new mc2 ();
    var 
    mymc3:mc3 = new mc3 ();
    var 
    mymc4:mc4 = new mc4 ();

    var 
    theArray:Array = new Array (mymc1mymc2mymc3mymc4);//Array of all the movieclips
    var shuffledArray:Array = new Array ();//Soon to be array of shuffled movieclips

    //Make the shuffled array
    while (theArray.length 0) {
        var 
    whichMC:Number Math.floor (Math.random () * theArray.length);
        
    shuffledArray.push (theArray [whichMC]);
        
    theArray.splice (whichMC1);
    }

    //Add the first movieclip from the array onto the stage
    this.addChild (shuffledArray [0]);

    var 
    currentMC:uint 1;

    //Add the Event.ENTER_FRAME event listener that runs every frame
    //24 fps means this event runs 24 times per second
    this.addEventListener (Event.ENTER_FRAMEonStageFramefalse0true);

    //What to do every time the event runs
    function onStageFrame (e:Event) {
        if (
    this.numChildren 0) {//If there's a movieclip on the stage
            
    var playingmc:* = this.getChildAt (0);//"Retrieve" it pretty much
        
            //If the movieclip has reached the end of its animation
            
    if (playingmc.currentFrame == playingmc.totalFrames) {
                
    this.removeChild (playingmc);//Remove it from the stage
                
                //If there is still at least one possible choice of movieclips to add
                
                    
    this.addChild (shuffledArray [currentMC]);//Add the movieclip

    // add to currentMC so we will play the next MovieClip next time
    currentMC += 1;

    // check if currentMC is bigger than the number of MCs we have. If so, start over
    if (currentMC shuffledArray.length) {
    currentMC 0
    }
                    
                
            }
        }

    ok, that should do it. after it shuffles, it plays it over and over in the same order.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center