Archive for the ‘ActionScript’ Category

Below you can find source code for effect named Clownfish. Probably some of you will scratch your heads when seeing this name but for me watching how these bubles avoid cursor is similar to how small clownfish finds shelter in stinging tentacles of the anemone.

You can find it in action at this website (circles over top menu).

/**
 * Copyright (c) 2011, Marek Bedkowski
 * All rights reserved.
 *
 * @author Marek Bedkowski
 * @package pl.bedkowski.code.clownfish
 * @license new BSD License (https://github.com/bendi/code/blob/master/zend_form_decorator_js_validation/LICENSE)
 *
 */
package pl.bedkowski.code.clownfish
{
	import flash.display.MovieClip;
	import flash.events.Event;

	public class ClowFishBall extends MovieClip
	{
		private var distance:Number,startX:Number = 0,startY:Number = 0,speed:Number = 0;

		private static var DISTANCE:Number = 3000;

		private static var FILL_COLOR:Number = 0x13698F;

		public function ClowFishBall(_x:Number, _y:Number, radius:Number)
		{
			graphics.beginFill(FILL_COLOR);
			graphics.drawCircle(0,0,radius);
			graphics.endFill();

			startX = x = _x;
			startY = y = _y;

			speed = (20/height)*height;
			addEventListener(Event.ENTER_FRAME, followMouse);
		}

		private function followMouse(e:Event):void
		{
			var xdiff:Number = MovieClip(parent).mouseX - x;
			var ydiff:Number = MovieClip(parent).mouseY - y;
			var dist:Number = Math.sqrt(xdiff * xdiff + ydiff * ydiff);
			if (dist < DISTANCE)
			{
				var angle:Number = Math.atan2(ydiff,xdiff);
				var sp:Number = (speed/dist)*3;
				x = startX - Math.cos(angle) * sp;
				y = startY - Math.sin(angle) * sp;
			}
		}
	}
}

And here’s how it’s been generated:

import pl.bedkowski.code.clownfish.ClowFishBall;

var dist = 10;
for(var ii=0,j=0;ii<9;ii++) {
	for(var i=15,h=1;i<310;i+=dist,j++,h+=.3) {
		var mc:MovieClip =  new ClowFishBall(i, 5+dist*ii, h/2);
		addChild(mc);
	}
}

Example:

LICENSE