To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-15-2004, 07:25 PM   #1
hi_world
Junior Member
 
Join Date: Jul 2004
Posts: 9
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 07:36 PM.
hi_world is offline   Reply With Quote
Old 10-15-2004, 07:40 PM   #2
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
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 08:55 PM.
jbum is offline   Reply With Quote
Old 10-15-2004, 08:38 PM   #3
hi_world
Junior Member
 
Join Date: Jul 2004
Posts: 9
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.
hi_world is offline   Reply With Quote
Old 10-15-2004, 08:52 PM   #4
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
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 08:57 PM.
jbum is offline   Reply With Quote
Old 10-15-2004, 09:02 PM   #5
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
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();
}
};

__________________
jbum is offline   Reply With Quote
Old 10-16-2004, 09:38 AM   #6
hi_world
Junior Member
 
Join Date: Jul 2004
Posts: 9
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!
hi_world is offline   Reply With Quote
Old 01-11-2006, 03:12 PM   #7
shinobispider
Junior Member
 
Join Date: Jan 2006
Posts: 1
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
shinobispider is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:37 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.