Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

isOutsideOf.as

Name: isOutsideOf() - tests if a movieclips bounds is out of the bounds of a movieclip or getBounds object
Author: senocular: www.senocular.com
Date: 1899-12-31T00:36:49.000
Documentation:
MovieClip ISOUTSIDEOF: checks to see if a movieclip
outside of 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 tested to be 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.isOutsideOf(myBounds);

Example:
onClipEvent(enterFrame){
	// generic move movieclip around function 
	this.moveAboutRandomly();
	// fade a movieclip out if not within the box
	// fade back in when in the box
	if (this.isOutsideOf(_root.box)) _alpha--;
	else _alpha++;
}

1
2
3
4
5
6
7
MovieClip.prototype.isOutsideOf = function(cb){
	if (typeof cb == "movieclip") return !this.hitTest(cb);
	var tb = this.getBounds();
	if (this._x + tb.xmax < cb.xmin || this._x + tb.xmin > cb.xmax) return true;
	if (this._y + tb.ymax < cb.ymin || this._y + tb.ymin > cb.ymax) return true;
	return false;
}