Name: keepOutsideOf() - keeps movieclip outside the an object bounds or a movieclip bounds
Author: senocular: www.senocular.com
Date: 1899-12-31T00:37:30.400
Documentation:
MovieClip KEEPOUTSIDEOF: keeps a movieclip within the bounds
of a) another movieclip or b) a bounds object in the form of
an object as retrieved from the movieclip.getBounds() method
Arguments:
- cb: or 'onstraint bounds', the bounds to which this movieclip is to be kept out of.
This can be either a movieclip or a getBounds object. As a getBounds object it doesnt have
to be an object specifically from a getBounds action, but it has to be in the same format,
which is an object containing the 4 following properties
xmin - the 'left' of the bounds, how far left this clip can go
xmax - the 'right' of the bounds, how far right this clip can go
ymin - the 'top' of the bounds, how high this clip can go
ymax - the 'bottom' of the bounds, how low this clip can go
ex:
myBounds = {xmin:0,xmax:200,ymin:10,ymax:90};
this.keepOutsideOf(myBounds);
Example:
onClipEvent(enterFrame){
// generic move movieclip around function
this.moveAboutRandomly();
// keep clip within the box area
this.keepOutsideOf(_root.box);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MovieClip.prototype.keepOutsideOf = function(cb){
if (typeof cb == "movieclip") cb = cb.getBounds(this._parent); // constraint bounds
var tb = this.getBounds(this._parent); // this bounds
var inx = !((tb.xmax < cb.xmin) ^ (tb.xmin > cb.xmax)); // inside bounds along x
var iny = !((tb.ymax < cb.ymin) ^ (tb.ymin > cb.ymax)); // inside bounds along x
if (inx && iny){
var tcx = (tb.xmin + tb.xmax)/2, ccx = (cb.xmin + cb.xmax)/2; // this center x, bounds center x
var tcy = (tb.ymin + tb.ymax)/2, ccy = (cb.ymin + cb.ymax)/2; // this center y, bounds center y
var xd = Math.abs(ccx - tcx), yd = Math.abs(ccy - tcy); // x difference, y difference
if (xd > yd){
if (tcx < ccx) this._x = Math.min(this._x, cb.xmin+this._x-tb.xmax);
else this._x = Math.max(this._x, cb.xmax+this._x-tb.xmin);
}else{
if (tcy < ccy) this._y = Math.min(this._y, cb.ymin+this._y-tb.ymax);
else this._y = Math.max(this._y, cb.ymax+this._y-tb.ymin);
}
}
}