PDA

View Full Version : random & swf



MiSs_Ge
06-08-2008, 11:23 PM
Hi! I need help!

I had a code for add 2 .swf in my html. Now I want 3 .swf but my code doesn't work!! Someone wants help me please? :D


___________________________________

// Generate a Random Number
var randomnumber = Math.round(Math.random()*3);

// Select a movie and execute the corresponding function
if (randomnumber == 0)

{movie1();
}

else

{movie2();
}

else

{movie3();
}


//Functions to write out the correct flash movie resource.

function movie1(){
document.write("<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width=\'855\' height=\'500\'><param name=movie value='index.swf'><param name=quality value=high><embed src='index.swf' quality=high pluginspage=\'http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\' type=\'application/x-shockwave-flash\' width=\'855\' height=\'500\'></embed></object>")
}

function movie2(){
document.write("<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\' width=\'855\' height=\'500\'><param name=movie value='index2.swf'><param name=quality value=high><embed src='index2.swf' quality=high pluginspage=\'http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\' type=\'application/x-shockwave-flash\' width=\'855\' height=\'500\'></embed></object>")
}

function movie3(){
document.write("<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\' width=\'855\' height=\'500\'><param name=movie value='index3.swf'><param name=quality value=high><embed src='index3.swf' quality=high pluginspage=\'http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\' type=\'application/x-shockwave-flash\' width=\'855\' height=\'500\'></embed></object>")
}


___________________________________

I can't find my error... Thx!

Nile
06-09-2008, 12:10 AM
Here we go, I fixed and cleaned it up for you.


// Generate a Random Number
var movieDis; //global variable
var number = 2; //your number of videos -1
var randomnumber = Math.round(Math.random()*number+1); //generate a random number between 1-number+1
movie(randomnumber);

//Functions to write out the correct flash movie resource.
function movie(num){
if(num == 1){
movieDis = "<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width=\'855\' height=\'500\'><param name=movie value='index.swf'><param name=quality value=high><embed src='index.swf' quality=high pluginspage=\'http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\' type=\'application/x-shockwave-flash\' width=\'855\' height=\'500\'></embed></object>";
} else if(num == 2){
movieDis = "<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\' width=\'855\' height=\'500\'><param name=movie value='index2.swf'><param name=quality value=high><embed src='index2.swf' quality=high pluginspage=\'http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\' type=\'application/x-shockwave-flash\' width=\'855\' height=\'500\'></embed></object>";
}else if(num == 3){
movieDis = "<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\' width=\'855\' height=\'500\'><param name=movie value='index3.swf'><param name=quality value=high><embed src='index3.swf' quality=high pluginspage=\'http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\' type=\'application/x-shockwave-flash\' width=\'855\' height=\'500\'></embed></object>";
} else {
movieDis = "There was an error. Please try again."; //error
}
document.write(movieDis);
}


When you want to add another movie, the highlighted is where you want to add another number. That indicates the number of movies -1, so for now you've got 3 mvoies, so I put it at 2, 3-1=2(if you didn't know. ;)). To add another movie, simply add:


}else if(num == x){
movieDis = "null for now. :D";

Before the } else {. The first highlighted is a variable x, thats the number indicating what number movie it is. And the second highlighted "null for now. :D" is where your gonna want to put your object.
Also I see that if the randomnumber equals 0 in your script, it will go to movie1(same as in my script). So I think that this is gonna function right and exactly how yours does, except... Well... Without the part that it doesn't work. :)

MiSs_Ge
06-09-2008, 12:16 AM
It's work! :D
Thank you!!

Nile
06-09-2008, 12:22 AM
I also updated my post 5 minutes after you posted. It gives a quick tutorial. That my be useful in the future. ;)
I also updated the code to be a bit neater. So use the updated.