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

 

senocular.com ActionScript Library

keepInIntersectOf.as

Name: keepInIntersectOf() - stretches a mc to be contained in the intersection of 2 other clips
Author: senocular: www.senocular.com
Date: 1899-12-31T00:22:08.100
Documentation:
Movieclip KEEPININTERSECTOF: Stretches a movieclip to
the intersecting bounding box area of two other movieclips.

Arguments:
- a: first movieclip to be used in intersection
- b: second movieclip to be used in intersection
 
Warnings:
- this prototype uses _height and _width to adjust the size of the clip this is
used on.  Because of that, _rotation to this clip must be avoided. If the clip
needs to be rotated, put that clip in a non-rotating container and use this on
that container instead.

Example:
// any movieclip
onClipEvent(mouseMove){
	if (Key.isDown(1)){
		// move this clip with the mouse
		_x = _parent._xmouse;
		_y = _parent._ymouse;
		updateAfterEvent();
		// when this clip intersects clipc, clipa is stretched
		// to encompass the intersected area 
		_root.clipa.keepInIntersectOf(this, _root.clipc)
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
MovieClip.prototype.keepInIntersectOf = function(a,b){
	var ab = a.getBounds(this._parent); // clip a bounds
	var bb = b.getBounds(this._parent); // clip b bounds

	var l = Math.max(ab.xMin, bb.xMin); // left
	var r = Math.min(ab.xMax, bb.xMax); // right
	var b = Math.max(ab.yMin, bb.yMin); // bottom
	var t = Math.min(ab.yMax, bb.yMax); // top
	
	this._width = Math.max(0, r-l);
	this._height = Math.max(0, t-b);
	
	if (!this._width || !this._height){ 
		this._width = this._height = 0;
	}else{ // if 0 size, no point in moving
		var tb = this.getBounds(this._parent); // this bounds
		this._x = this._x - tb.xMin + l;
		this._y = this._y - tb.yMax + t;
		if (this._rotation) this._rotation = 0; // make sure this _rotation is 0
	}
}