Moving mc's randomly

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?

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

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 :slight_smile:

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 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"

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 :slight_smile:

Cheers

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.

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

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

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(aa+bb));
}
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.

hmm… going to have to think about this.

hey! look what i found in my old archive =)

might help … :wink:

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

And just 8 lines instead of my millions of them :slight_smile:

Thanks kax

no problem schling :wink:

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 :wink:

But how do I adjust the speed of the mc in the clip?

in the prototype there’s a line like this:

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

.1 would be the speed

Yeah, that did the trick.

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

stupid maths :stuck_out_tongue:

maybe this will solve it

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;

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.

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 :wink:

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;
}

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 :wink: 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.