# Controlling random movement

**URL:** <https://forum.kirupa.com/t/controlling-random-movement/179883>\
**Category:** Uncategorized\
**Created:** [February 23, 2006, 6:39pm UTC](https://forum.kirupa.com/t/controlling-random-movement/179883 "2006-02-23T18:39:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![michelle\_b](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@michelle\_b](https://forum.kirupa.com/u/michelle_b)\
**Post date:** [February 23, 2006, 6:39pm UTC](https://forum.kirupa.com/t/controlling-random-movement/179883/1 "2006-02-23T18:39:27Z")

</div>

Hi all,

I am using the code below to give two mc’s random movement, the thing is the movement is too broad, I want to set a limit on how far they move. Can I set a boundry? Or set a maximum position?

Frame 1:

```auto

//set loops variable to 0
loops = 0;
//set the target x and y to random 450
_root.target_x = Math.random()*450;
_root.target_y = Math.random()*300;
// /30 is the amount of steps taken to get there, the higher the
//amount the smoother the transition
// div is equal to the target xy coordinates minus the xy coordinate of the mc
_root.leftxdiv = (_root.target_x-_root.left_mc._x)/100;
_root.leftydiv = (_root.target_y-_root.left_mc._y)/100;

//same as above only rooted to different mc
_root.target_x = Math.random()*450;
_root.target_y = Math.random()*300;
_root.rightxdiv = (_root.target_x-_root.right_mc._x)/50;
_root.rightydiv = (_root.target_y-_root.right_mc._y)/50;

```

Frame 2:

```auto

//++ increment
loops++;
//+= addition assignment
//add left x coord to the root leftxdiv result taken from frame 1
_root.left_mc._x += _root.leftxdiv;
_root.left_mc._y += _root.leftydiv;
//same as above but for right_mc
_root.right_mc._x += _root.rightxdiv;
_root.right_mc._y += _root.rightydiv;

```

Frame 3:

```auto
//again increment loops
//same code as in previous frame up until if statement to create smoother tween
//Q. Could the if statment not be put on the previous frame removing this script?
loops++;
_root.left_mc._x += _root.leftxdiv;
_root.left_mc._y += _root.leftydiv;
_root.right_mc._x += _root.rightxdiv;
_root.right_mc._y += _root.rightydiv;
//if statement, if loops are less that 20 go to frame 2
//going to frame two will loop these two frames until 20 is reached
if (loops<20) {
    gotoAndPlay(2);
//if not go to and play 1, going to frame one will reset loops back to 0
//thus continuing the script
} else {
    gotoAndPlay(1);
}

```

Any help would be appreciated.

Thanks

M X :hugegrin:
