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.
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.
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 (mc1, mc2, mc3);
//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.
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 (mc1, mc2, mc3);
//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.
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.
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!!!
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...
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
}
}
}
}
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]);
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_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
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.