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

 

senocular.com ActionScript Library

wave.as

Name: wave() - moves clip along a sine wave
Author: senocular: www.senocular.com
Date: 1899-12-31T00:05:06.100
Documentation:
MovieClip WAVE: positions a clip to move along a
wave path, either horizontal or vertical based on sine.
 
Arguments:
- axis: axis, as string, either "x" or "y" to move wave along. if "x" or "y" is not
given "x" is assumed.
- amplitude: amplitude of the wave; half the height.
- wavelength: wavelength of the wave; the full length traveld before wave repeats.
- position: position along wave in radians.  Math.PI*2 is one full wavelengths worth of movement.
- xoffset and yoffset: (optional) postion of the wave's starting point or the wave's origin.
Default is 0;

Example:
// moves clip in wave pattern starting at x=0, y=150
// along the x axis at a speed of .1 radians per frame
// the wave has an amplitude of 20 and wavelength of 100
onClipEvent(load){
	time = 0;
}
onClipEvent(enterFrame){
	time += .1;
	this.wave("x", 20, 100, time, 0, 150);
}

1
2
3
4
5
6
7
8
9
10
11
12
MovieClip.prototype.wave = function(axis, amplitude, wavelength, position, xoffset, yoffset){
	if (arguments.length < 5) var xoffset = 0, yoffset = 0;
	var move = wavelength * position/(Math.PI*2);
	var wave = Math.sin(position) * amplitude;
	if (axis.toLowerCase() == "y" || axis.toLowerCase() == "_y"){
		this._y = move + yoffset;
		this._x = wave + xoffset;
	}else{
		this._x = move + xoffset;
		this._y = wave + yoffset;
	}
}