all groups > flash actionscript > january 2004 >
You're in the

flash actionscript

group:

randomazation code


randomazation code BuG56
1/12/2004 11:03:19 PM
flash actionscript:
NOTE THAT I USE FLASH 5(I DO USE MX BUT PREFER NOT TO USE IT UNLESS IT IS NECCESSARRY)

now i have recently learned about the randimazation code and since most of the people here are proffessionals, i thought you could help me.

lets say i have 3 movie clips, with the instance names A,B, and C, how do i use actionscript to randomly place the three clips in random areas on the screen, wait for 5 seconds, and then dissapear



on a side note, how do you controll sounds with actionscript?

any help would be appreciated.

Re:randomazation code juice002
1/13/2004 3:12:22 AM

Let's say the size of your stage is 600x400, all the movie clips have a reference point at their top-left corner, and you want the clips to be placed so they are completely visible.

A._x = random(600 - A._width);
A._y = random(400 - A._height);

B._x = random(600 - B._width);
B._y = random(400 - B._height);

C._x = random(600 - C._width);
C._y = random(400 - C._height);

Now, to delay for 5 seconds, the preferred way to do this is with setInterval (only be available on MX)...

setInterval(clearClips, 5000);

function clearClips()
{
A.removeMovieClip();
B.removeMovieClip();
C.removeMovieClip();
}

A less reliable alternative to setInterval is to use onEnterFrame and a counter. Let's say your frame rate is 24 fps...

delay_counter = 24*5;
onEnterFrame = function() {

if (delay_counter-- <= 0) {
clearClips();
onEnterFrame = undefined;
}
}



Rich





Re:randomazation code BuG56
1/14/2004 4:17:38 AM
ok thanks, but i have 1 more question, if i want them to randomly spawn at certain locations locations only, how do i do that, and how do i find the cooridnates for the area?

once again any help would be appreciated.


Re:randomazation code sneakyimp
1/14/2004 10:32:15 AM
if you want the items to spawn at certain locations only, you are going to need to define these areas by some kind of formula...if you want a square, it's easy...it's more difficult if you're talking about circular regions or parabolas or something.

i use this code to randomly create objects on a movie object...the objects (sparky little stars in my case) are small (so their dimensions don't really matter) but they all appear to be stuck to the object in question which is a square:.

FRAME 1 SCRIPT
===========
_global.intSparkleDensity = 15;
_global.intCurrentSparkleID = 0;
_global.intDepth = this.getDepth()+1;
_global.intMaxX = this._width; // this value limits randomly assigned X values -- the can't exceed the dimensions of the object
_global.intMaxY = this._height; // this value limits randomly assigned Y values
this.onEnterFrame = function() {
for (i=0; i<intSparkleDensity; i++) {
intCurrentSparkleID++;
if (intCurrentSparkleID>1000) {
intCurrentSparkleID = 0;
}
strSparkleID = String(intCurrentSparkleID);
this.attachMovie("one_sparkle", strSparkleID, 0+intCurrentSparkleID);
intRndX = Math.random()*intMaxX; //generates random number between 0 and intMaxX
intRndY = Math.random()*intMaxY; //generates random number between 0 and intMaxY
this[strSparkleID]._x = intRndX;
this[strSparkleID]._y = intRndY;
}
};
===========


in frame 3, i have a goto loop
======
this.gotoAndPlay(this._currentframe-1);
======


hope this helps...

AddThis Social Bookmark Button