logo

[SOLVED]: Random image trick

Stuck with scripting ? Find your answers here.

Postby smartrem » Fri Dec 08, 2006 3:20 pm

Hi!
I made a simple swish movie wich loads a random image in its folder. I saved everythind in a folder called image1. The swf works fine by itself.
In dreamweaver, I want to add my swf. When I try the page, the movie can't find the images in the image1 folder.
If I paste the images in the same folder as the page in I am trying, the all thing works fine.
Two solutions for me :
- I force the movie to load the image from the folder (I don't know how to specify that in my code)
- I change the code to pick an image called 0.jpg to n.jpg because I want to make several movies in the same page that load random images (but they don't have all the same size.
I think the second solution is the best, so how do I specify in the mathrandom argument the range of numbers it has to choose from?

Did I really make myself clear??
User avatar
smartrem
 
Posts: 40
Joined: Wed Feb 02, 2005 9:56 am
Location: France

Postby emory » Fri Dec 08, 2006 3:30 pm

Have you tried the help section of Swish?
It has a good example of random loading.
Code: Select all
Arrays and Random
Top  Previous  Next  


Description
Demonstrates how to use Arrays to display a random message.

Aim
Introduces methods for Arrays and Math.randomInt()


1. Create a new file and save it as "mymaxarray.swi". Insert a Dynamic text object and name it "message" on the 'Text' panel. Be sure to tick the 'Target' option so that it can be used in our script.



2. Right-Click on the text object in the 'Outline' panel and select Grouping | Group as Sprite from the menu. Name the sprite "s_message" on the 'Sprite' panel.



3. With the sprite object selected in the 'Outline' panel, open the 'Script' panel and press the 'Add Script' button. Select Events | Frame | onLoad() from the menu.

4. Right-Click on the onLoad() event and select Add Script | Statements | evaluate; from the menu.



5. Type "smt = new Array()" into the evaluate field on the 'Script' panel as shown below:



Note: In the script above, a new empty array object was created and assigned to the variable "smt".

6. Press the 'Add Script' button again and select Statements | evaluate; to add our first value to this array. In the evaluate field type what is shown on the image below:



Note: First the variable "smt" is used with brackets after it. The number inside the square brackets indicates the position (or index) within the array (please note, array indices always start from zero as the first element of the array). The message "awesome!" is defined for this position in the array.

7. Repeat Step #6 to add several more values to this array. Make sure that you increase the number inside the brackets by one each time a new position in the array is defined. It may be faster to copy and paste the first value and simply change the values. An example is shown below:



8. When you have created several new values for the array, press the 'Add Script' button again and select Statements | name=expr; from the menu. In the 'Target' field use the drop-down menu to select the dynamic text object "message" from the list of targets. In the 'Name' field, use the drop-down menu to select Text | text. Leave the Operator as "=" and begin typing what is shown in the image below "SWiSHmax is" etc. When you type the first bracket, Right-Click after it and select Math | Math.randomInt({max}) from the menu as shown below:



9. When the Math.randomInt() script is added, the cursor will be inside the parentheses. Type the number "8" (which is one number higher than the last number in the array) into the parentheses. Then finish the script by adding the closing bracket "]". The 'Script' panel should appear similar to the image below:



10. Go back to the 'Layout' panel and press the 'Play' button in the Control Toolbar to preview the movie. Press the 'Play' button several times to see a random message displayed.

Analysis:
The sprite "s_message" contains on onLoad() event which is triggered when the sprite is first loaded. Inside the onLoad() event a new Array object was defined. Array indices begin with 0 - zero - as the first position in the array and a value is defined for each position. Using the Math.randomInt() code, the movie will select a number between 0 and the number inserted into parentheses (note, the number in parentheses will not be one of the random numbers which is why we used a value one greater than the last position in the array).

The script that contains "smt[Math.randomInt()]" will created a random number for the array and the number that is picked will correspond with one of the positions in the array.




Let us know.
User avatar
emory
 
Posts: 2997
Joined: Fri Feb 27, 2004 3:56 pm
Location: Georgia, USA

Postby smartrem » Fri Dec 08, 2006 3:41 pm

Ok,
I've been working on my second solution and I used this code
Code: Select all
onload(){
  loadmovie(Math.randomRange(0,2)add ".jpg");
    }

My last (I hope) problem is that the math.random doesn't pick 0, 1 or 2 but 0.21546 or 1.2541
See what I mean?

I got it!
I used this
Code: Select all
onload(){
  loadmovie(Math.round(Math.randomRange(0,2))add ".jpg");
    }

The math.round pick a round number from the range I gave.
So now I'll make a second movie which will pick from 3 to 5 and so on...
Thanks, I hope this will help someone.
User avatar
smartrem
 
Posts: 40
Joined: Wed Feb 02, 2005 9:56 am
Location: France

Re: [SOLVED]: Random image trick

Postby sweeshdude » Sun Jan 24, 2010 12:14 am

Sometimes random isn't actually random as it can still load an image previously just loaded. I am wondering if we can set a Random Check somewhere in the codes to avoid previously loaded pic being loaded again...?

At least, the Random check can skip once, so there's no chance for previously loaded pic being loaded right away.

For example, I have a Function something like:

function pickRandom() {
HolderArray = new Array();
ListNumbers = new Array("1", "2", "3", "4","5");

for(i = 0; i < 10; i++) {

makeRandom = Math.floor(Math.random() * (ListNumbers));
pickedNumber = ListNumbers[makeRandom];

// maybe we can check here whether the current pickedNumber is the same with just the previous one.
// if not, put in HolderArray. if the same, pick again, but the numbers must remain 10 numbers.

HolderArray[i] = pickedNumber;

}
}

This Function works, and it gives me 10 "random" numbers. However, I can still see two same numbers next to each other which I consider the random picks the same number twice in a row.

How can we avoid this...?
sweeshdude
 
Posts: 3
Joined: Mon Jan 18, 2010 6:18 pm


Return to Swishmax & SwishScript Help

Who is online

Users browsing this forum: No registered users and 2 guests