# Moving mc's randomly

**URL:** <https://forum.kirupa.com/t/moving-mcs-randomly/15361>\
**Category:** Uncategorized\
**Created:** [March 13, 2003, 11:56am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361 "2003-03-13T11:56:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![schling](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@schling](https://forum.kirupa.com/u/schling)\
**Post date:** [March 13, 2003, 11:56am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/1 "2003-03-13T11:56:44Z")

</div>

Hello

I want to have a few movie clips moving randomly over my flashsite. They will look bugs. And I want them to move in the direction the head is pointing.

I have tried the random movement tutorial here on Kirupa but can’t get the mc to rotate in the direction it’s moving.

Does anyone know how to do this?

---

<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:** [March 13, 2003, 2:49pm UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/2 "2003-03-13T14:49:36Z")

</div>

Hi Schling,  
You might try posting your fla so that people can see what you are trying to do and help you fix it - it doesn’t sound to me like that big a problem, but that is easy to say when i cannot see the file

Cheers

SteveD

---

<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:** [March 13, 2003, 4:30pm UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/3 "2003-03-13T16:30:45Z")

</div>

Okey here is the \*.fla file.

First: It’s a bit messy since I have been messing around with it, so I hope you can make some sense out of it.

Second: The animation isn’t a bug yet, just a button looking thingy with “feet” in front of it. You’ll se it. I need those “feet” on the front of the mc.

Thanks 🙂

---

<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:** [March 14, 2003, 6:37pm UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/4 "2003-03-14T18:37:19Z")

</div>

Hi Schling,  
I was going take a look at your file today, but all hell let loose here and …well you know what they say about good intentions.  
However, all is not lost, I have just found this thread [here](http://www.kirupaforum.com/forums/attachment.php?s=&postid=122013) there is a file there that may solve your probs, the character turns to face the way he is moving, however the movement is fired on a mouseclick - shouldn’t be too hard to convert - if its not what you want, post here and I’ll have look over the w/e - hope it helps

SteveD

oops sorry, that link doesn’t work - its is in the AS forum under the heading " game character movement"

---

<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:** [March 15, 2003, 11:05am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/5 "2003-03-15T11:05:25Z")

</div>

Hi

I found the thread and downloaded the fla file. And I will take a look at it tonight.

Hopefully I will be able to convert it to my own script 🙂

Cheers

---

<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:** [March 16, 2003, 11:11pm UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/6 "2003-03-16T23:11:13Z")

</div>

Well, I took a look at the fla file, but I have still have problems converting it to work with my own file.

I think I know where my problem is though. All my roation codes are based on the random math for the movement. And therefore it also roatates in a random direction. The thing I think I need is a code that bases it self on the speed on the x and y axis.

---

<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:** [March 19, 2003, 9:22am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/7 "2003-03-19T09:22:01Z")

</div>

Can’t anyone help me 8-\ ???

---

<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:** [March 19, 2003, 9:31am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/8 "2003-03-19T09:31:28Z")

</div>

What is the code from the fla you downloaded? I can’t download it here at work.

---

<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:** [March 19, 2003, 9:47am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/9 "2003-03-19T09:47:55Z")

</div>

This is the code is in layer 1, frame 1 on my scene.

function getdistance(x, y, x1, y1) {  
var run, rise;  
run = x1-x;  
rise = y1-y;

```
return (_root.hyp(run, 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 = 400;  
height = 400;  
//-------------------  
var dist, norm;  
this.x = this.\_x;  
this.y = this.\_y;  
this.speed = Math.random()\*4+1;  
this.targx = Math.random()\*width;  
this.targy = Math.random()\*height;  
dist = \_root.getdistance(this.x, this.y, this.targx, this.targy);  
norm = this.speed/dist;  
this.diffx = (this.targx-this.x)\*norm;  
this.diffy = (this.targy-this.y)\*norm;  
//this.\_rotation = this.x , this.y

};  
MovieClip.prototype.move = function() {  
if (\_root.getdistance(this.x, this.y, this.targx, this.targy)\>this.speed) {  
this.x += this.diffx;  
this.y += this.diffy;  
} else {  
this.x = this.targx;  
this.y = this.targy;  
if (!this.t) {  
this.t = getTimer();  
}  
if (getTimer()-this.t\>1000) {  
this.reset();  
this.t = 0;  
}  
}

```
this._x = this.x;
this._y = this.y;

```

};

This code is one MC

onClipEvent(enterFrame){  
move();  
}

PS I use Flash MX, perhaps I should have told people that ealier.

---

<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:** [March 19, 2003, 10:02am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/10 "2003-03-19T10:02:31Z")

</div>

hmm… going to have to think about this.

---

<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:** [March 19, 2003, 11:17am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/11 "2003-03-19T11:17:50Z")

</div>

hey! look what i found in my _old archive_ =)

might 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:** [March 19, 2003, 11:41am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/12 "2003-03-19T11:41:48Z")

</div>

That was a simple code, and it looks like I can use it 🙂

And just 8 lines instead of my millions of them 🙂

Thanks kax

---

<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:** [March 19, 2003, 11:48am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/13 "2003-03-19T11:48:12Z")

</div>

no problem **schling** 😉

> And just 8 lines instead of my millions of them

seriously … that’s a bit too much code for such a little thing :-\

**suprabeener** is a great scripter and i respect that, i really do =)  
but i also think that the code should be updated to something _easier_ 😉

---

<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:** [March 19, 2003, 11:55am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/14 "2003-03-19T11:55:25Z")

</div>

But how do I adjust the speed of the mc in the 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:** [March 19, 2003, 11:59am UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/15 "2003-03-19T11:59:54Z")

</div>

in the prototype there’s a line like this:

```auto
Math.ceil(this._x) == ... : this._x += (this.x-this._x) ***.1** ;

```

**.1** would be the _speed_

---

<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:** [March 19, 2003, 12:09pm UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/16 "2003-03-19T12:09:21Z")

</div>

Yeah, that did the trick.

But it apperaes to stop after a while if I have the speed number lower than .05

---

<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:** [March 19, 2003, 12:12pm UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/17 "2003-03-19T12:12:56Z")

</div>

stupid maths 😛

maybe this will solve it

```auto
Math.ceil(this._x) == this.x || Math.floor(this._x) == this.x ? this.x = Math.round(Math.random()*whatever) : this._x += (this.x-this._x)*.1;

```

---

<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:** [March 19, 2003, 12:24pm UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/18 "2003-03-19T12:24:20Z")

</div>

I repleaced that code with the code I have for X, and re-wrote it for Y aswell. But it still stops when it’s lower than .05.

Strange, because it looks like it just dies.

---

<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:** [March 19, 2003, 12:28pm UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/19 "2003-03-19T12:28:42Z")

</div>

crap … :hair:

look … i don’t have the time right now to rewrite the scrip and fix it  
but i know [size=1]at least in theory :P[/size] that if you add this line to suprabeener’s code it should do the trick 😉

```auto
MovieClip.prototype.move = function() {
	if (_root.getdistance(this.x, this.y, this.targx, this.targy)>this.speed) {
		this.x += this.diffx;
		this.y += this.diffy;
		**this._rotation = Math.atan2(this.diffy, this.diffx)*180/Math.PI;**
	} else {
		this.x = this.targx;
		this.y = this.targy;
		if (!this.t) {
			this.t = getTimer();
		}
		if (getTimer()-this.t>1000) {
			this.reset();
			this.t = 0;
		}
	}
	this._x = this.x;
	this._y = this.y;
}

```

---

<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:** [March 19, 2003, 12:40pm UTC](https://forum.kirupa.com/t/moving-mcs-randomly/15361/20 "2003-03-19T12:40:12Z")

</div>

Sorry Kax, but that code didn’t work. It rotates the mc all right, but in a random direction. And not the way it’s facing…

But don’t feel stressed about script 😉 If you wan’t to solve it, take your time. It’s no rush. I have a 1000 other things that has to be made on the page.

[Next page](https://forum.kirupa.com/t/moving-mcs-randomly/15361.md?page=2)
