PDA

View Full Version : moving mc's randomly



schling
March 13th, 2003, 06:56 AM
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?

SteveD
March 13th, 2003, 09:49 AM
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

schling
March 13th, 2003, 11:30 AM
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 :-)

SteveD
March 14th, 2003, 01:37 PM
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"

schling
March 15th, 2003, 06:05 AM
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

schling
March 16th, 2003, 06:11 PM
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.

schling
March 19th, 2003, 04:22 AM
Can't anyone help me 8-\ ???

upuaut
March 19th, 2003, 04:31 AM
What is the code from the fla you downloaded? I can't download it here at work.

schling
March 19th, 2003, 04:47 AM
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.

upuaut
March 19th, 2003, 05:02 AM
hmm.. going to have to think about this.

kode
March 19th, 2003, 06:17 AM
hey! look what i found in my old archive =)

might help .. ;)

schling
March 19th, 2003, 06:41 AM
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

kode
March 19th, 2003, 06:48 AM
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 ;)

schling
March 19th, 2003, 06:55 AM
But how do I adjust the speed of the mc in the clip?

kode
March 19th, 2003, 06:59 AM
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

schling
March 19th, 2003, 07:09 AM
Yeah, that did the trick.

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

kode
March 19th, 2003, 07:12 AM
stupid maths :P

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;

schling
March 19th, 2003, 07:24 AM
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.

kode
March 19th, 2003, 07:28 AM
crap .. :hair:

look .. i don't have the time right now to rewrite the scrip and fix it
but i know at least in theory :P that if you add this line to suprabeener's code it should do the trick ;)

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

schling
March 19th, 2003, 07:40 AM
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.

kode
March 19th, 2003, 07:49 AM
actually it works schling :)
i was like what the .. !!? it should work! and tested it ;)

by the way .. i'll fix my script later and i'm not stressed out about it ;)

upuaut
March 19th, 2003, 08:06 AM
thanks kax

kode
March 19th, 2003, 08:08 AM
originally posted by david
thanks kax
ehmm .. yeah. i don't know why but you're welcome =)

schling
March 19th, 2003, 08:35 AM
Well, I won't even start to guess why it wouldn't work on my script. Since most of the codes are the same on both. Exept that you don't have any code on the mc, just an instance name.

But! Yours work, and I can use it. Just wish I could really understand why mine didn't work. Guess I must take a good look at it.

Thanks Kax, and to all others that have tried to help me :) I'll post url for my page as soon as it's done.

upuaut
March 19th, 2003, 08:39 AM
"I don't know why but your'e welcome."

because you saved me the trouble of thinking about the code of course. :)

kode
March 19th, 2003, 08:40 AM
Exept that you don't have any code on the mc, just an instance name.
at the end of the code

mc.onEnterFrame = move;
it's the same than

onClipEvent (enterFrame) {
this.move();
}


But! Yours work, and I can use it. Just wish I could really understand why mine didn't work. Guess I must take a good look at it.
can you post the exact code you used? i might find what's wrong with it ;)

and again .. no problem schling =)

kode
March 19th, 2003, 08:44 AM
because you saved me the trouble of thinking about the code of course :)
oh i see .. =)

i should get paid for this :P jk ;)

schling
March 20th, 2003, 04:27 AM
Here it is.

This is my modified code, with your input.
Don't know why it won't work, perhaps it even will at your computer.

kode
March 20th, 2003, 04:42 AM
mine ..

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

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._rotation = Math.atan2(this.x, this.y)*360/Math.PI;
this._x = this.x;
this._y = this.y;
};

schling
March 20th, 2003, 04:46 AM
Ops :-)

what the hell does that line there?
So, THAT is why mine didn't work.

You see I had the same line you used before, just at the wrong place. That is what I find really annoying. But now my script is all messy....

kode
March 20th, 2003, 04:52 AM
you can have the line there if you want to .. it shouldn't be a problem :)
just remove the other line ;)


by the way .. i just realize that your line is wrong :-\

this._rotation = Math.atan2(this.x, this.y)*360/Math.PI;
shoul be 180 not 360 ..

but i can fix that ;)

this._rotation = Math.atan2(this.diffy, this.diffx)*360/(Math.PI*2);
=)

schling
March 20th, 2003, 04:57 AM
Yup, used 180 myself at first, but when it didn't work I tried to use 360. Since I thought that it would go 360 degrees, while 180 it would only go half the way in the circel.

But as I said, my old script was filled with mess. Guess I scripted it do death :-\

kode
March 20th, 2003, 05:02 AM
But as I said, my old script was filled with mess. Guess I scripted it do death :-\
:P .. don't worry, it happens to everyone ;)