-
Random move...orient to path?
How do I make my MC point in the direction its moving?
This is the code
code:
acceleration = 10;
newpos = function () { ranx = Math.round((Math.random()*250));rany = Math.round((Math.random()*250));};
newpos();
this.onEnterFrame = function() {
this._x += ((ranx-this._x)/acceleration);
this._y += ((rany-this._y)/acceleration);
if (Math.round(this._x) == ranx || Math.round(this._y) == rany) {
newpos();
}
};
EDIT: I added [ as ] tags to your code for readability. See the forum guidelines. - jbum
Last edited by jbum; 10-15-2004 at 06:36 PM.
-
Senior Member
Here's a rewrite of your code that includes rotation in the direction of movement.
I also changed the method of detecting if you've reached your destination.
code:
acceleration = 10;
newpos = function ()
{
ranx = Math.round((Math.random()*250));
rany = Math.round((Math.random()*250));
};
newpos();
this.onEnterFrame = function()
{
// figure direction of movement, relative to current position
var dx = ranx-this._x;
var dy = ranx-this._y;
// figure angle in radians
var ar = Math.atan2(dy,dx);
// convert to rotation in degrees
this._rotation = ar*180/Math.PI;
this._x += dx/acceleration;
this._y += dy/acceleration;
if (Math.abs(dx) < .5 || Math.abs(dy) < .5) {
newpos();
}
};
Last edited by jbum; 10-15-2004 at 07:55 PM.
-
well its not working?
I just visited your website...and what I would like...is something like your "bee" (It pointing the way its moving).
Im trying to make a moviclip, with a butterfly flying randomly over screen.
-
Senior Member
A couple things:
1) I forgot to mention that with this code, your 'normal' movieclip (with rotation at zero) should be pointing to the right.
If by 'not working' you mean that your movieclip was pointing 90 degrees to the left of where it should be, then that was the problem.
If you can't fix the movieclip to point to the right, you can add a constant to the rotation to compensate for this:
this._rotation = ar*180/Math.PI + 90;
If by 'not working' you mean something else (such as an error message), please be clear about how it's not working, do I can help you fix it.
2) There was a syntax error in my code which I have since fixed (an extra paren).
Finally, I'm guessing you want your movieclip to turn smoothly instead of flipping around quickly, as it is now.
This is a bit more complicated and I'll post it shortly.
Last edited by jbum; 10-15-2004 at 07:57 PM.
-
Senior Member
Here's code which smoothly turns the object. Again, the 'normal' object should be pointed the right.
code:
acceleration = 5;
newpos = function ()
{
ranx = random(250);
rany = random(250);
};
newpos();
mc.onEnterFrame = function()
{
// figure direction of movement, relative to current position
var dx = ranx-this._x;
var dy = ranx-this._y;
// figure angle in radians
var ar = Math.atan2(dy,dx);
// convert to rotation in degrees
var da = ar*180/Math.PI; // assumes normal movieclip is pointing to the right - add 90 after this step if it is pointing up.
da -= this._rotation;
// these lines prevent critter from turning the wrong way around
while (da > 180) da -= 360;
while (da < -180) da += 360;
this._rotation += da/acceleration;
this._x += dx/acceleration;
this._y += dy/acceleration;
if (Math.abs(dx) < 1 || Math.abs(dy) < 1) {
newpos();
}
};
-
Okay the first script...Mc pointing wrong direction...like you said!
Im now trying with the new code....and it is looking better. It still do a little movement pointing wrong, but it looks better....thanks mate! appreciated!
-
I would like to do something similar to this, having a small movie clip randomly moving about the workspace, tho i'm new to using actionscript and am unsure if the code given here is all i need or not? Also unsure whether the actionscript goes in the first frame keyframe or the movieclip or somewhere entirely different? Thanks for any help with this
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|