4 Replies Latest reply on Jun 5, 2013 1:52 AM by Zaxist

# How to randomize position of many duplicate symbols

I have a rectangle (Named "Data") that falls down the stage on a loop. This is all contained within the symbol itself. The stage has this code in it:

var randX;

var randShouldWait;

var randSpeed;

var baseXVal = 215;

function randomNumbers() {

randX = Math.floor(Math.random()*6);

randShouldWait = Math.floor(Math.random()*3);

randSpeed = Math.floor(Math.random()*100);

moveMe();

}

randomNumbers();

function moveMe(){

if (randShouldWait == 1) {

randShouldWait = Math.floor(Math.random()*3);

sym.\$("Data").animate({left: 0 + 20 * randX, top: sym.\$("Data").y}, 150 + randSpeed, moveMe);

}

else

sym.\$("Data").animate({left: 0 + 20 * randX, top: sym.\$("Data").y}, 150 + randSpeed, randomNumbers);

}

This creates a nice looking randomized "shifting" as the block falls. The issue is that this code is specifically related to the "Data" object and when I duplicate the symbol it names it "Data_1" which obviously doesn't shift randomly. The goal is to have this expandable to have dozens of these rectangles at once, so Ideally I'd like to find a way to create new ones programmatically that each have individualized behavior so my timeline isn't so busy.

• ###### 1. Re: How to randomize position of many duplicate symbols

i think if you simply use classes then you dont have any problem

to use class add Dot ( . ) before your Data name like below

function moveMe(){

if (randShouldWait == 1) {

randShouldWait = Math.floor(Math.random()*3);

sym.\$(".Data").animate({left: 0 + 20 * randX, top: sym.\$(".Data").y}, 150 + randSpeed, moveMe);

}

else

sym.\$(".Data").animate({left: 0 + 20 * randX, top: sym.\$(".Data").y}, 150 + randSpeed, randomNumbers);

}

then simply just add Data class to your symbols that you want ...

after you click on your symbol to add class you should click on the C letter that is front of symbol title in the left panel and type your class name in the box that is open for you and hit the enter

if you had problem, let me know

Zaxist

• ###### 2. Re: How to randomize position of many duplicate symbols

Ok this makes both of the symbols move, but they are moving in sync with each other, which isn't what I want. I was kind of hoping to find a way to move this code from the stage to the symbols themselves so that when they are duplicated they each contain their own randomized functions with unique random numbers.

• ###### 3. Re: How to randomize position of many duplicate symbols

Hi, James-

Use the creationComplete event in the symbol (one of the two events that are available on the symbol level) to add your symbol randomization code.  It serves as an initialization method.

Hope that helps,

-Elaine

• ###### 4. Re: How to randomize position of many duplicate symbols

ok if you want to make them move seperatly try this way:

w = "Data_1"

y = "Data_2"

z = "Data_3"

var randX;

var randShouldWait;

var randSpeed;

var baseXVal = 215;

function randomNumbers(f) {

randX = Math.floor(Math.random()*6);

randShouldWait = Math.floor(Math.random()*3);

randSpeed = Math.floor(Math.random()*100);

moveMe(f);

}

function moveMe(x){

if (randShouldWait == 1) {

randShouldWait = Math.floor(Math.random()*3);

sym.\$(x).animate({left: 0 + 20 * randX, top: sym.\$(x).y}, 150 + randSpeed, moveMe);

}

else

sym.\$(x).animate({left: 0 + 20 * randX, top: sym.\$(x).y}, 150 + randSpeed, randomNumbers);

}

randomNumbers(w)

randomNumbers(y)

randomNumbers(z)

and you can check it by repeating functions, to repeat just replace last three lines with these:

// will Repeat every 151 milisec

function retry(){

randomNumbers(w)

randomNumbers(y)

randomNumbers(z)

}

setInterval(retry,151);

Zaxist