15 Replies Latest reply on Jun 17, 2010 10:37 PM by kglad

# How to limit the movement of my mc??

i have a movie clip moving randomly...but i would like to put a limit or boundary on its movement...

the problem is...the bondary is a round area and i don't know how to calculate the area...

is it possible to make a script that says...while inside this certain object, they will move randomly...

as of now this is my code:

var speed:Number = 4;                                             //this object shouldn't go beyond the boundary
object_mc.onEnterFrame = function() {
if (Key.isDown(Key.RIGHT)) {
this._x = this._x+speed;
} else if (Key.isDown(Key.LEFT)) {
this._x = this._x-speed;
}
if (Key.isDown(Key.UP)) {
this._y = this._y-speed;
} else if (Key.isDown(Key.DOWN)) {
this._y = this._y+speed;
}
};

//////////////////////////////////////////////////////////////////////////////////////
function getdistance(x, y, x1, y1) {                                        //and so as this one
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 = 650;
height = 488;
//-------------------
var dist, norm;
this.x = this._x;
this.y = this._y;
this.speed = Math.random()*4+2;
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;
};
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;
};
//////////////////////////////////////////////////////////////////////////////////////
stop();

• ###### 1. Re: How to limit the movement of my mc??

no, you'll need to encode that.

• ###### 2. Re: How to limit the movement of my mc??

i put this in the whole code:

if (eval(this._droptarget) == bgback_mc) {

}

but the object doesnt move randomly....it just flash to different places...

• ###### 3. Re: How to limit the movement of my mc??

You can use either hittest or Circle formula to find whether the particle is moving inside the circle or reached boundry

Simple circle formula

###### r  = Math.sqrt( x*x - y*y)

Using this we can determine whether the particle is moving inside a circle or not. we can easily get the radius of the circle (Width / 2) also we can determine the Center point of the circle. If the center point is not 0,0

var xD = centerX - clip_mc._x;

var yD = centerY - clip_mc._y;

##### r = Math.sqrt ( Math.pow(xD , 2) + Math.pow(yD , 2))

this r will be the radius of the circle. So If the value is equal to the circle radius means the particle hits the circle boundry, if the value is grater than the Radius it goes beyond the boundry otherwise is ir moving inside the boundry.

Using an enterframe or setinterval move the particle and check the particle r using the formula above.

• ###### 4. Re: How to limit the movement of my mc??

you don't want to check for a _droptarget.  you could use a distance formula (suggested above) or you could use a hittest.

• ###### 5. Re: How to limit the movement of my mc??

i'm quite new to this...i'm not sure how to put this in my code...

hope you guys could help...

• ###### 6. Re: How to limit the movement of my mc??

which code snippet are you using?  you showed two unrelated code snippets in your first message.

• ###### 7. Re: How to limit the movement of my mc??

the 2nd one...

the one with function()

• ###### 8. Re: How to limit the movement of my mc??

i used the first code to controlo my main character using arrow keys....

the second code is for my enemies to move randomly....

actually i couldn't really understand the 2nd code either, i just got this one from a tutorial...

• ###### 9. Re: How to limit the movement of my mc??

then if you're trying to keep those enemies within a circle of radius r centered at centerX, centerY, you can use:

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

if(_root.getdistance(this.x,this.y,centerX,centerY)<r){

this._x = this.x;
this._y = this.y;

} else {

// do something.  "this" is about to move to or past circle boundary

}
};

• ###### 10. Re: How to limit the movement of my mc??

i used this code but the enemy stopped moving....

• ###### 11. Re: How to limit the movement of my mc??

did you define the variables r, centerX and centerY?  if so, show your code.

• ###### 12. Re: How to limit the movement of my mc??

its working now..thanks a lot KGLAD!!!

how about in my first code??? i also need to put the same boundary...

• ###### 13. Re: How to limit the movement of my mc??

:

var speed:Number = 4;                                             //this object shouldn't go beyond the boundary

object_mc.onEnterFrame = function() {
if (Key.isDown(Key.RIGHT)) {
this._x = this._x+speed;
} else if (Key.isDown(Key.LEFT)) {
this._x = this._x-speed;
}
if (Key.isDown(Key.UP)) {
this._y = this._y-speed;
} else if (Key.isDown(Key.DOWN)) {
this._y = this._y+speed;
}

if(_root.getdistance(this._x,this._y,centerX,centerY)>r){

// do something.  "this" is about to  move to or past circle boundary

}

};

• ###### 14. Re: How to limit the movement of my mc??

hi kglad!! i put the function in my code but my main character doesn't move...

• ###### 15. Re: How to limit the movement of my mc??

that's not because of the code i suggested.  until/unless you put something where that comment is, nothing will be change.