Random Generated Displacement Map

So,  I was following this tutorial from activetuts+ to create the same effect,  a static distortion effect using the displacement map filter,  in one of my game projects and it was cool until I realized that my project is getting fatter and fatter by adding images of different sizes to use for different sized objects.

Ofcourse, this was not acceptable so I thought I could create a random generated image by code and reuse it than to create different images in Photoshop.

Anyway, here’s the code which is generating a random image to be used with the effect in the tutorial.

function rndImage(w:Number, h:Number, wl:int = 0, bl:int =0):BitmapData
{
var bitmapdata:BitmapData = new BitmapData(w,h,false, 0xDDDDDD);
var randY:int = 0;
var randX:int = 0;
var randW:int = 0;
var thick:Number = 0;
var a:int = 0;
var minWidth:int = 0;
var line:Shape = new Shape();

for ( a = 0; a< wl; a++)
{
thick = Math.random()* 1.3 + 0.2;
randY = Math.random() * h;
minWidth = (40 * w)/100;
randW = Math.random() * (w-minWidth) + minWidth;
randX = Math.random() * ((w-randW));
line.graphics.lineStyle(thick, 0xFFFFFF);
line.graphics.moveTo(randX,randY);
line.graphics.lineTo(randW, randY);
}

for ( a = 0; a< bl; a++)
{
thick = Math.random()* 1.3 + 0.2;
randY = Math.random() * h;
minWidth = (40 * w)/100;
randW = Math.random() * (w-minWidth) + minWidth;
randX = Math.random() * ((w-randW));
line.graphics.lineStyle(thick, 0x000000);
line.graphics.moveTo(randX,randY);
line.graphics.lineTo(randW+randX, randY);
}

bitmapdata.draw(line);

return bitmapdata;
}

And this is how you generate the image

rndImage(width,height, nr_white_lines,nr_dark_lines);

Detect Mouse Inactivity in Actionscript 3.0

Before we start you need to be familiar with MouseEvent and Timer classes.

var _timer:Timer = new Timer(5000);

function Start():void
{
   _timer.addEventListener(TimerEvent.TIMER, ShowMsg, false, 0, true);
   _timer.start();

   stage.addEventListener(MouseEvent.MOUSE_MOVE, StopTimer, false, 0, true);
   stage.addEventListener(MouseEvent.MOUSE_DOWN, StopTimer, false, 0, true);
   stage.addEventListener(MouseEvent.MOUSE_UP, StopTimer, false, 0, true);
}

function ShowMsg(e:TimerEvent):void
{
   trace("There is no activity");
}

function StopTimer(e:MouseEvent):void
{
   _timer.stop();
   _timer.start();
}

Start();

The first thing I’ve done is to setup a Timer object to count up to 5 seconds ( you can change that to what ever numer of seconds you want ).

The timer object also has an event listener so we can know when the 5 seconds are over.

I have setup some event listeners that will help detecting any actions of the mouse on the Stage ( MOUSE_DOWN, MOUSE_MOVE and MOUSE_UP )

If during those 5 seconds nothing happens ( mouse is not moved or performs a click ) ShowMsg() function will be fired up and give you a message of inactivity.

If the mouse is moved the StopTimer() function will be activated and reset timers count.

This is a useful and easy way to detect user activity.

Tween A Number with GreenSock’s Tweening Class

I am going to give you a little example on how to tween a number. This example can be used with other tweening classes as well like Caurina’s Tweener.

Pay attention that the variable we want to tween needs to be public otherwise you’ll get an error compiling the code.

We are going define a variable as an Object. This object will hold a value. The tweener will tween this object’s value.

public var my_number :Object  =  { value: 0 };
TweenLite.to( this.my_number,  .5, { value: 100 , onUpdate:function(){ trace (this.value); } } )

 

Pie Preloader Animation in Actionscript 3.0

PiePreloader.as 

package
{
	import flash.display.Shape;

	public class PiePreloader extends Shape
	{
		private var _radius:Number = 0;
		private var _bc    :int    = 0;
		private var _fc    :int    = 0;
		private var _bs    :int    = 0;

		public function PiePreloader($radius:Number, $border_size:int, $border_color:int, $fill_color:int)
		{
			_radius = $radius;
			_bs     = $border_size;
			_bc     = $border_color;
			_fc     = $fill_color;
		}

		public function Draw($e:Number):void
		{
			var $end   = 2*Math.PI*$e/100;

			this.graphics.clear();

			if ($end != 0)
			{
				this.graphics.lineStyle(_bs,_bc);
				this.graphics.beginFill(_fc);
				this.graphics.lineTo(_radius,0);

				Curve(0, 0, 0, $end);

				this.graphics.lineTo(0,0);
				this.graphics.endFill();
			}
		}

		private function Curve($x:Number, $y:Number, $start:Number, $end:Number)
		{
			 var diff = Math.abs($end -$start);
			 var divs = Math.floor(diff/(Math.PI/4))+1;
			 var span = -(diff/(2*divs));
			 var rc   = _radius/Math.cos(span);

			 this.graphics.moveTo(Math.cos($start)*_radius, Math.sin($start)*_radius);

			 for (var i=0; i<divs; ++i)
			 {
				  $end = $start+span;
				  $start = $end+span;
				  this.graphics.curveTo(Math.cos($end)*rc, Math.sin($end)*rc, Math.cos($start)*_radius, Math.sin($start)*_radius);
			 };

			 return this;
		};
	}
}

How to use it:

var _preloader:PiePreloader = new PiePreloader(35, 1, 0x000000, 0x74C2E1);
_preloader.x = stage.stageWidth/2;
_preloader.y = stage.stageHeight/2;
addChild(_preloader);


var _loader:Loader = new Loader();
_loader.load(new URLRequest("punk.jpg"));
_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, Progress, false, 0, true);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,         Complete, false, 0, true);


function Progress($e:Event):void
{
	var $p:Number = $e.target.bytesLoaded/$e.target.bytesTotal*100;
	_preloader.Draw($p);
}

function Complete($e:Event):void
{
	addChild(_loader)
}