# Random Animation

**URL:** https://forum.kirupa.com/t/random-animation/710
**Category:** flash
**Created:** [July 26, 2002, 12:00pm UTC](https://forum.kirupa.com/t/random-animation/710 "2002-07-26T12:00:42Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![NANA](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@NANA](https://forum.kirupa.com/u/NANA)
#### Post date: [July 26, 2002, 12:00pm UTC](https://forum.kirupa.com/t/random-animation/710/1 "2002-07-26T12:00:42Z")

</div>

HOW DID PUT MOVIES AT RANDOM WHEN THE USER IS NO INTERACTING WITH PAGE(EX.IF THE USER IS NOT CLICKING ANY MENU, A ANMATION SHOULD BE PLAYED. CAN ANY ONE HELP:)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 26, 2002, 1:19pm UTC](https://forum.kirupa.com/t/random-animation/710/2 "2002-07-26T13:19:37Z")

</div>

=) there is a solution =)  
create 2 layers,one actions and other is objects. 🙂  
then in the actions layer put in this code

function getdistance (x, y, x1, y1) {  
var run, rise;  
run = x1-x;  
rise = y1-y;  
return (hyp(run, rise));  
}  
function hyp (a, b) {  
return (Math.sqrt(a_a+b_b));  
}  
MovieClip.prototype.reset = function () {  
var dist, norm, movie\_height, movie\_width;

// movie\_height: refers to the height of your movie  
// movie\_width: refers to the width of your movie

//---------------------------------

movie\_height = 600;  
movie\_width = 950;

//---------------------------------

speed = Math.random()_4+2;  
targx = Math.random()_(movie\_width-\_width);  
targy = Math.random()\*(movie\_height-\_height);  
dist = \_root.getdistance(\_x, \_y, targx, targy);

norm = speed/dist;  
diffx = (targx-\_x)\*norm;  
diffy = (targy-\_y)\*norm;  
};

MovieClip.prototype.move = function () {  
var cycle;

// cycle: specifies the number of milliseconds waited  
// between movements

//--------------------------------------------

cycle = 200;

//--------------------------------------------

if (\_root.getdistance(\_x, \_y, targx, targy)\>speed) {x += diffx;y += diffy;  
} else {x = targx;  
y = targy;  
if (!this.t) {  
t = getTimer();  
}if (getTimer()-t\>cycle) {reset();  
t = 0;  
}  
}  
\_x = x;  
\_y = y;  
}

then on the other layer create a movie clip which will move randomly.

give it this code.

onClipEvent (enterFrame) {  
move();  
}

then put this code in any other mc’s you want to have moved randomly,but you only need 1 actions layer with 1 single action in this forum reply,ok hope this is use full 4 u.  
=) =)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 26, 2002, 1:28pm UTC](https://forum.kirupa.com/t/random-animation/710/3 "2002-07-26T13:28:32Z")

</div>

Nice code DMX… Can you explain a bit?=)

pom :asian:

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 26, 2002, 5:51pm UTC](https://forum.kirupa.com/t/random-animation/710/4 "2002-07-26T17:51:39Z")

</div>

function getdistance (x, y, x1, y1) {  
var run, rise;  
run = x1-x;  
rise = y1-y;  
return (hyp(run, rise));  
}  
function hyp (a, b) {  
return (Math.sqrt(a_a+b_b));  
}  
MovieClip.prototype.reset = function () {  
var dist, norm, movie\_height, movie\_width;

// movie\_height: refers to the height of your movie  
// movie\_width: refers to the width of your movie

//---------------------------------

movie\_height = 600;  
movie\_width = 950;

//---------------------------------

is self explaining.

speed = Math.random()_4+2;  
targx = Math.random()_(movie\_width-\_width);  
targy = Math.random()\*(movie\_height-\_height);  
dist = \_root.getdistance(\_x, \_y, targx, targy);

norm = speed/dist;  
diffx = (targx-\_x)\*norm;  
diffy = (targy-\_y)\*norm;  
};

MovieClip.prototype.move = function () {  
var cycle;

// cycle: specifies the number of milliseconds waited  
// between movements

is info about speed and stuff.

//--------------------------------------------

cycle = 200;

//--------------------------------------------

if (\_root.getdistance(\_x, \_y, targx, targy)\>speed) {x += diffx;y += diffy;  
} else {x = targx;  
y = targy;  
if (!this.t) {  
t = getTimer();  
}if (getTimer()-t\>cycle) {reset();  
t = 0;  
}  
}  
\_x = x;  
\_y = y;  
}

decides movements and maximum num of mc’s moving.

ps.see attached movie random.zip

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 26, 2002, 9:14pm UTC](https://forum.kirupa.com/t/random-animation/710/5 "2002-07-26T21:14:45Z")

</div>

There’s no attached file. And it’s the first time I see someone on this forum thinking that Supra’s code is self-explanatory…

Anyway, NANA, the tute is on this site, just look for “random motion tutorial”.

pom :asian:
