# Random Motion!

**URL:** https://forum.kirupa.com/t/random-motion/5612
**Category:** flash
**Created:** [September 17, 2002, 5:19pm UTC](https://forum.kirupa.com/t/random-motion/5612 "2002-09-17T17:19:15Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jayjay](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@jayjay](https://forum.kirupa.com/u/jayjay)
#### Post date: [September 17, 2002, 5:19pm UTC](https://forum.kirupa.com/t/random-motion/5612/1 "2002-09-17T17:19:15Z")

</div>

I’m trying to make add this little movie clip to my page but I can’t seem to get it to work properly. What I’m trying to do is this. I have a bunch of short lines and i want them to move up and down smoothly and continously. I looked at the random motion tut on the site but it didn’t look like what I was after.

Can some one help?

Thanks

PS. you can view the site at www.jcgsolutions.ca so you can have a look at what I’m talking about. The movie clip is in the upper right corner.

jayjay

---

<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: [September 17, 2002, 5:54pm UTC](https://forum.kirupa.com/t/random-motion/5612/2 "2002-09-17T17:54:46Z")

</div>

Just remove EVERYTHING that relates to the \_x position of the object. Here… I edited the script for what you want to do, Tried, tested, and true it works…

```auto
//special thanks to Suprabeener for the code
function getdistance(y, y1) {
	var rise;
	rise = y1-y;
	return (_root.hyp(rise));
}
function hyp(a, b) {
	return (Math.sqrt(a*a+b*b));
}
MovieClip.prototype.reset = function() {
	//specify the width and height of the movie
	width = 300;
	height = 400;
	//-------------------
	var dist, norm;
	this.y = this._y;
	this.speed = Math.random()*4+2;
	this.targy = Math.random()*height;
	dist = _root.getdistance(this.y, this.targy);
	norm = this.speed/dist;
	this.diffy = (this.targy-this.y)*norm;
};
MovieClip.prototype.move = function() {
	if (_root.getdistance(this.y, this.targy)>this.speed) {
		this.y += this.diffy;
	} else {
		this.y = this.targy;
		if (!this.t) {
			this.t = getTimer();
		}
		if (getTimer()-this.t>1000) {
			this.reset();
			this.t = 0;
		}
	}
	this._y = this.y;
};

```

Just replace the frame actions with the actions above and it should work the way you wanted.

To change the speed just change this line…  
this.speed = Math.random()\*4+2;

---

<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: [September 17, 2002, 6:13pm UTC](https://forum.kirupa.com/t/random-motion/5612/3 "2002-09-17T18:13:22Z")

</div>

Thanks…

I can’t seem to get it to work though. Maybe I’m not adding the code to right spot. Do I put in the main timeline or in the movie clip itself?

Thanks again…I’ll keep trying.

jayjay

---

<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: [September 17, 2002, 6:18pm UTC](https://forum.kirupa.com/t/random-motion/5612/4 "2002-09-17T18:18:17Z")

</div>

That code goes on the main timeline. On a Frame.

The code given to you for the movie clip is in the tutorial, I believe it is something like this…

```auto
onClipEvent(enterFrame){
	move();
}

```

So the first code I gave you goes on the timeline in a Frame and the code above goes directly on your movie clip.

---

<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: [September 17, 2002, 6:28pm UTC](https://forum.kirupa.com/t/random-motion/5612/5 "2002-09-17T18:28:58Z")

</div>

Thanks alot!! it’s working fine now! I should’ve known to do that.

jayjay

---

<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: [September 17, 2002, 6:31pm UTC](https://forum.kirupa.com/t/random-motion/5612/6 "2002-09-17T18:31:45Z")

</div>

It is good that you got it working:)
