Random image generator works great..!

  • torbjorn
  • Novice
  • Novice
  • User avatar
  • Joined: Aug 21, 2003
  • Posts: 23
  • Status: Offline

Post April 9th, 2004, 2:50 pm

Just figured out how to make an image (within Flash) come up different each time you click refresh on the browser...
Example at:
http://www.neverbeat.com/flash

On the first frame after the preloader:
Code: [ Download ] [ Select ]
num = (Math.random())*10;
//generates a random nuber between 0 and 1 and multiplies it by a factor of 10
rand = Math.round(num);
//rounds it to a whole number
if (rand==0) {
    rand = 1;
}
//excludes 0
loadMovie("image_movie"+rand+".swf",_root.target1);
//concatinates the name of the loaded clip with the random number and //appends the file extension '.swf', then loads a clip into a target clip.
  1. num = (Math.random())*10;
  2. //generates a random nuber between 0 and 1 and multiplies it by a factor of 10
  3. rand = Math.round(num);
  4. //rounds it to a whole number
  5. if (rand==0) {
  6.     rand = 1;
  7. }
  8. //excludes 0
  9. loadMovie("image_movie"+rand+".swf",_root.target1);
  10. //concatinates the name of the loaded clip with the random number and //appends the file extension '.swf', then loads a clip into a target clip.


Then all you have to do is make 10 clips called 'image_movie1.swf' through 'image_movie10.swf' within the same directory.
Sooo, when you click refresh on the browser, the clips load in a random order, showing a different picture each time a visotor comes to your site.
Sweet!
t
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post April 9th, 2004, 2:50 pm

  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Joined: Jun 26, 2003
  • Posts: 1402
  • Loc: Philadelphia, PA
  • Status: Offline

Post April 9th, 2004, 8:24 pm

Shorter method:

Code: [ Download ] [ Select ]
_root.target1.loadMovie("image_movie"+(random(10)+1)+".swf");



Get's a random number between 1 and 10 and adds it into the string directly.


random(num) gets a number value from 0 up to that number (so in the case of 10, it will return 0-9), adding 1 to it will produce from 1 up to and including the number used.
  • dcastro
  • Newbie
  • Newbie
  • No Avatar
  • Joined: May 16, 2005
  • Posts: 6
  • Status: Offline

Post May 16th, 2005, 1:27 pm

Does the image have to be a swf? could the same coded for a jpg directly?

thanks
Dax


Ideally I have a set of 4 photo spots and have been bustin my balls to find a script that will random rotate 4 at a time based on images in a folder. But so far all I have are sore balls.

LOL.

HELP!!
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Joined: Jun 26, 2003
  • Posts: 1402
  • Loc: Philadelphia, PA
  • Status: Offline

Post May 19th, 2005, 8:58 am

It would be the same for a jpg.... however the jpg has to be saved as non-progressive (typically an option when you save as jpeg in a graphics program).
  • dcastro
  • Newbie
  • Newbie
  • No Avatar
  • Joined: May 16, 2005
  • Posts: 6
  • Status: Offline

Post May 19th, 2005, 9:06 am

So instead of just one image I would like 3 or 4 on the same page. How would I code that?
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Joined: Jun 26, 2003
  • Posts: 1402
  • Loc: Philadelphia, PA
  • Status: Offline

Post May 19th, 2005, 9:10 am

If I understand correctly you would have to create 4 seperate movieclip symbols to load to. Give them each seperate instance names, place them on the stage where they need to be, then call the loadMovie() code to each of the clips to load the image you want to load.
  • dcastro
  • Newbie
  • Newbie
  • No Avatar
  • Joined: May 16, 2005
  • Posts: 6
  • Status: Offline

Post May 19th, 2005, 9:12 am

great thanks. It could be the same clip just different instance names though right?
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Joined: Jun 26, 2003
  • Posts: 1402
  • Loc: Philadelphia, PA
  • Status: Offline

Post May 19th, 2005, 10:19 am

Yes indeed :)
  • R3LzX
  • Born
  • Born
  • No Avatar
  • Joined: May 05, 2007
  • Posts: 1
  • Status: Offline

Post May 5th, 2007, 8:38 am

I know this is an old topic but I was wondering if someone had this in fla format for me to download. I am having trouble making the actual loader portion of it (maybe cacause I am a total noob to flash and have no idea what i am doing)
  • krismeister
  • Graduate
  • Graduate
  • No Avatar
  • Joined: Oct 21, 2006
  • Posts: 202
  • Status: Offline

Post May 9th, 2007, 4:30 pm

Do you have a movie clip called target1?

As in:
_root.target1

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 59 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2010 Unmelted, LLC. Driven by phpBB © 2010 phpBB Group.