How can I confine this to a square?

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:

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.

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

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.

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

Excellent, thanks for the help dude :smiley:

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…

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

Produces the same effect.

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

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

*Originally posted by Marc *

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 :slight_smile:

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.

Thanks for the advice, I learnt a lot there.

:slight_smile: