Random image generator works great..!

  • torbjorn
  • Novice
  • Novice
  • User avatar
  • Posts: 23

Post 3+ Months Ago

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: [ 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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Shorter method:

Code: [ 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
  • dcastro
  • Posts: 6

Post 3+ Months Ago

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
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

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
  • dcastro
  • Posts: 6

Post 3+ Months Ago

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
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

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
  • dcastro
  • Posts: 6

Post 3+ Months Ago

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

Post 3+ Months Ago

Yes indeed :)
  • R3LzX
  • Born
  • Born
  • R3LzX
  • Posts: 1

Post 3+ Months Ago

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
  • krismeister
  • Posts: 202

Post 3+ Months Ago

Do you have a movie clip called target1?

As in:
_root.target1
  • Karl2012
  • Born
  • Born
  • Karl2012
  • Posts: 2

Post 3+ Months Ago

Ok I've been trying to create creating a flash tarot card program using this as a guideline. I've been using 4 cards on the stage card1, card2 etc and a predict movie clip and I'm going to create 50 tarot swfs for this. I've tried the following action script for card1

ACTIONSCRIPT Code: [ Select ]
class card1 extends MovieClip
{
   function onLoad ()
   {
_root.Predict.onPress = function()
      {
         
         _root.card1draw();
      }
   }
function draw (){
num = (Math.random())*51;
    //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.card1);
   }
}
 
  1. class card1 extends MovieClip
  2. {
  3.    function onLoad ()
  4.    {
  5. _root.Predict.onPress = function()
  6.       {
  7.          
  8.          _root.card1draw();
  9.       }
  10.    }
  11. function draw (){
  12. num = (Math.random())*51;
  13.     //generates a random nuber between 0 and 1 and multiplies it by a factor of 10
  14.     rand = Math.round(num);
  15.     //rounds it to a whole number
  16.     if (rand==0) {
  17.         rand = 1;
  18.     }
  19.     //excludes 0
  20.     loadMovie("image_movie"+rand+".swf",_root.card1);
  21.    }
  22. }
  23.  


I get the error message an actionscript file must have at least one externally visible definition. Any ideas where I'm going wrong? Also I don't want the same image to come up more than once.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Try declaring your class as public and see if that resolves your issue. So:

public class card1 extends MovieClip
  • Karl2012
  • Born
  • Born
  • Karl2012
  • Posts: 2

Post 3+ Months Ago

I get the message that the public attribute can only be used inside a package. Possibly doing this the wrong way as it should be pretty easy to display 4 random images.

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 14 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.