# How can I confine this to a square?

**URL:** <https://forum.kirupa.com/t/how-can-i-confine-this-to-a-square/10003>\
**Category:** flash\
**Created:** [December 22, 2002, 11:23am UTC](https://forum.kirupa.com/t/how-can-i-confine-this-to-a-square/10003 "2002-12-22T11:23:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bodom78](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@Bodom78](https://forum.kirupa.com/u/Bodom78)\
**Post date:** [December 22, 2002, 11:23am UTC](https://forum.kirupa.com/t/how-can-i-confine-this-to-a-square/10003/1 "2002-12-22T11:23:52Z")

</div>

I have a ball that follows the mouse, but I need to confine it to a box shape. I can do it with the dragable buttons but not this MC that follows the mouse.

Here is the code I have attached to the ball MC:

```php
onClipEvent (load) {
    _x = 0;
    _y = 0;
    speed = 5;
}
onClipEvent (enterFrame) {
    targetx = _root._xmouse;
    targety = _root._ymouse;
}
onClipEvent (enterFrame) {
    _x += (targetx-_x)/speed;
    _y += (targety-_y)/speed;
}

```

Cheers.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 22, 2002, 2:09pm UTC](https://forum.kirupa.com/t/how-can-i-confine-this-to-a-square/10003/2 "2002-12-22T14:09:29Z")

</div>

Just one more step. Add this AS to the bottem of your AS:

```php
onClipEvent (enterFrame) {
	startDrag(getProperty (_x,_y), true, 1, 1, 200, 200);
}

```

This confines it to a 200 by 200 box.

There’s no need for your 2nd onClipEvent.

```php
onClipEvent (load) {
    _x = 0;
    _y = 0;
    speed = 5;
}
onClipEvent (enterFrame) {
    targetx = _root._xmouse;
    targety = _root._ymouse;
    _x += (targetx-_x)/speed;
    _y += (targety-_y)/speed;
}

onClipEvent (enterFrame) {
	startDrag(getProperty (_x,_y), true, 1, 1, 200, 200);
}

```

~Marc

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 22, 2002, 11:17pm UTC](https://forum.kirupa.com/t/how-can-i-confine-this-to-a-square/10003/3 "2002-12-22T23:17:51Z")

</div>

Excellent, thanks for the help dude 😃

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 22, 2002, 11:31pm UTC](https://forum.kirupa.com/t/how-can-i-confine-this-to-a-square/10003/4 "2002-12-22T23:31:33Z")

</div>

Well that completely disables the easing code so it doesn’t follow the mouse with easing…

And all of that can be squished down into this simple code…

```auto
onClipEvent (enterFrame) {
	startDrag(this, true, 1, 1, 200, 200);
}

```

Produces the same effect.

Now for constraining the easing code, it goes like this…

```auto
onClipEvent (load) {
	_x = 0;
	_y = 0;
	speed = 5;
}
onClipEvent (enterFrame) {
	targetx = _root._xmouse;
	targety = _root._ymouse;
	_x += (targetx-_x)/speed;
	_y += (targety-_y)/speed;
	if (this._x<=1) {
		this._x = 1;
	}
	if (this._x>=200) {
		this._x = 200;
	}
	if (this._y<=1) {
		this._y = 1;
	}
	if (this._y>=200) {
		this._y = 200;
	}
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 22, 2002, 11:37pm UTC](https://forum.kirupa.com/t/how-can-i-confine-this-to-a-square/10003/5 "2002-12-22T23:37:37Z")

</div>

> \*Originally posted by Marc \*

```php
onClipEvent (enterFrame) {
	startDrag(getProperty (_x,_y), true, 1, 1, 200, 200);
}

```

2 things: getProperty sucks and no need to startDrag on enterFrame, you just do it once (on Load for instance) and it’s enough.

pom 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 22, 2002, 11:39pm UTC](https://forum.kirupa.com/t/how-can-i-confine-this-to-a-square/10003/6 "2002-12-22T23:39:18Z")

</div>

Yes, getProperty does suck.

And Ilyas… thanks for the info, I always thought startDrag had to go on an enterFrame to update its position. I never tried on Load though.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 23, 2002, 1:10am UTC](https://forum.kirupa.com/t/how-can-i-confine-this-to-a-square/10003/7 "2002-12-23T01:10:47Z")

</div>

Thanks for the advice, I learnt a lot there.

🙂
