PDA

View Full Version : Making a mc follow another



leonthelion
December 27th, 2007, 03:54 PM
How can I make an mc move twords another mc's x position? That way it kind of follows it. I tried:

onClipEvent(enterFrame){
if(this._x<=circle._x){this._x+=5}} Which didn't work. I can't think of anything else though.

Jerryscript
December 27th, 2007, 04:39 PM
if(this._x != circle._x || this._y != circle._y){
this._x += (circle._x - this._x) /2;
this._y += (circle._y - this._y) /2;
}

leonthelion
December 28th, 2007, 12:26 AM
whats ! mean? Thanks for the code :D
EDIT:
heck whats the whole code mean :p
EDIT2:
forgot to say the squares not doing anything :(
square instance name: thingy
circle instance name: circle
code got put on square

leejay
December 29th, 2007, 08:24 PM
Hey Leon check http://www.neodroid.tk/ and find the 'Arcade Enemy AI Basics' tutorial from the scroll down. Hope this helps! I am also looking into a similar feature for a game I'm making but stuck on getting the enemy to follow you around walls and not get stuck behind them due to collision detection! can anyone help?

Tsukimaru
December 30th, 2007, 01:35 AM
whats ! mean? Thanks for the code :D
EDIT:
heck whats the whole code mean :p
EDIT2:
forgot to say the squares not doing anything :(
square instance name: thingy
circle instance name: circle
code got put on square

if(this._x != circle._x || this._y != circle._y){
this._x += (circle._x - this._x) /2;
this._y += (circle._y - this._y) /2;
}It basically means this: (! means not, and I know this's isn't a real word)
If the this's x value is not equal to the circle's x value, or if the this's y value is not equal to the circle's y value:
Add the circle's x minus this's x divided by 2 to this's x value.
Add the circle's y minus this's y divided by 2 to this's x value.

So if this's x is, let's say, 50, and the circle is to the right of it, with an x vaule of 100, the this would move to the x of 75 (100-50=50, 50/2=75), and if it was on an enterFrame handler, then it would move to 87.5, then 93.75, so it will keep getting closer, but it will never touch (although, it will look like it will have touched, but technically, it won't have).

Uh, I think I might have overexplained it. Heh. :D

leonthelion
January 1st, 2008, 04:01 PM
Sorry for not reading for awhile D:
Anyway I get the code now, but for some reason it's just not working.
The circles instance name is circle and the squares instance name is nothing (like nothing, not his instance name is nothing, he doesn't have one) I put this on the square, but he doesn't do anything:
onClipEvent(enterFrame){
if(this._x != circle._x || this._y != circle._y){
this._x += (circle._x - this._x) /2;
this._y += (circle._y - this._y) /2;
}}
Thanks