Check if class exists

October 2nd, 2009

With this little snippet you can check if a class exists could be quite handy while using library items with a linkage id.

1
2
3
4
5
if(ApplicationDomain.currentDomain.hasDefinition("TestClass")){
	trace("you can use TestClass :)");
}else{
	trace("TestClass can not be used");
}
Categories: Snippets

Bitmapdata displacement fishes

September 30th, 2009

With the displacement filter you can do a lot of cool effects, like in this example moving water.
It uses a bitmapdata with perlinNoise moved with the offset parameter to create the movement. Then with some moving fishes you got a nice little aquarium :)

A big downside of the displacement filter is that it is quite heavy.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var speed:int = 3;
var numfishes:int = 10;
 
var fishes:Array = new Array();
for(var i:int = 0; i < numfishes; i++){
	var f:Fish = new Fish();
	f.x = 50 + (Math.random() * 450);
	f.y = 50 + (Math.random() * 300);
	f.rotation = Math.random() * 360;
	mc.addChild(f);
	fishes.push(f);
	f.blendMode = BlendMode.OVERLAY;
	f.filters = [new BevelFilter(4, 45, 0xFFFFFF, .5, 0, .5)];
}
 
var offset:Array = [new Point(0,0), new Point(0,0)];
var displacebmd:BitmapData = new BitmapData(mc.width, mc.height);
var displacemap:DisplacementMapFilter = new DisplacementMapFilter(displacebmd, new Point(), 1, 1, 25, 25, "clamp");
 
function render(e:Event):void
{
	offset[0].x += speed;
	offset[1].y += speed;
	displacebmd.perlinNoise(100, 100, 1, 1, true, false, 0, true, offset);
	mc.filters = [displacemap];
 
	for each(var fish:Fish in fishes){
		fish.swim();
	}
}
this.addEventListener(Event.ENTER_FRAME, render);

Preview (could be a bit heavy on old pc’s):

Fishes

Categories: Actionscript

Lock your bitmapdata

September 28th, 2009

When you have a bitmapdata object and you are going to change a lot of it (for example give all the pixels a new color like the demo bellow) then you should lock it, when you locked your bitmapdata object all the changes you do are not rendered right away but only when you call the unlock method.
This can really boost the performance!

Example code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var uselock:Boolean;
var bmd:BitmapData = new BitmapData(550, 400);
var bmp:Bitmap = new Bitmap(bmd);
var field:TextField = new TextField();
field.x = 75;
field.width = 75;
field.height = 36;
field.background = true;
field.multiline = true;
 
addChild(bmp);
 
function render(e:Event):void
{
	if(uselock){
		bmd.lock();
	}
 
	for(var i:int = 0; i < 550; i++){
		for(var j:int = 0; j < 400; j++){
			bmd.setPixel(i, j, Math.random() * 0xFFFFFF);
		}
	}
 
	bmd.unlock();
}
stage.addEventListener(Event.ENTER_FRAME, render);
 
function togglelock(e:MouseEvent):void
{
	uselock = !uselock;
	field.text = "Click to toggle\nuselock: " + uselock.toString();
}
togglelock(null);
stage.addEventListener(MouseEvent.CLICK, togglelock);
addChild( new Stats() );
addChild(field);

When you have a verry fast pc you wont notice a lot at the example but it can make a difference of more then 5 fps!
The ugly but effective example:

Bitmapdata lock

And a big thanks to mr doob for the great stats!

Categories: Actionscript

Split rgb effect

September 26th, 2009

This effect is a effect that is used a lot in videos, i like this effect a lot so i thought why not make it with flash.
First i create a bitmapData object from a movieclip on the stage, then i split the red blue and green color channels into separate bitmapdata objects using the copyChannel method. I add those 3 as a bitmap into a holder and position the holder ontop of the original image that i hide.

The movement of the layers is a bit weird and hard but that is just for the demo, with a bit of tweening it is a great effect ;)

The code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function splitRGB(img:DisplayObjectContainer):Object
{
	var bmd:BitmapData = new BitmapData(img.width, img.height);
	bmd.draw(img);
	var r:BitmapData = new BitmapData(bmd.width, bmd.height, true, 0xFF000000);
	var g:BitmapData = new BitmapData(bmd.width, bmd.height, true, 0xFF000000);
	var b:BitmapData = new BitmapData(bmd.width, bmd.height, true, 0xFF000000);
	var rect:Rectangle = new Rectangle(0, 0, bmd.width, bmd.height);
	r.copyChannel(bmd, rect, new Point(0,0), BitmapDataChannel.RED, BitmapDataChannel.RED);
	g.copyChannel(bmd, rect, new Point(0,0), BitmapDataChannel.GREEN, BitmapDataChannel.GREEN); 
	b.copyChannel(bmd, rect, new Point(0,0), BitmapDataChannel.BLUE, BitmapDataChannel.BLUE);
	var h:Sprite = new Sprite();
	img.parent.addChild(h);
	h.x = img.x;
	h.y = img.y;
	img.visible = false;
	return {r:h.addChild(new Bitmap(r)), g:h.addChild(new Bitmap(g)), b:h.addChild(new Bitmap(b))};
}
 
var rgb:Object = splitRGB(img);
rgb.r.blendMode = BlendMode.SCREEN;
rgb.g.blendMode = BlendMode.SCREEN;
rgb.b.blendMode = BlendMode.SCREEN;
 
function update(e:Event):void
{
	var val:Number = Math.random() * 10;
	rgb.r.x = rgb.r.y = val;
	rgb.b.x = rgb.b.y = -val;
}
stage.addEventListener(Event.ENTER_FRAME, update);

The demo:

Split RGB

Categories: Actionscript

Parallax scrolling

September 24th, 2009

Parallax scrolling is a easy and great way to get depth in 2d side scrolling games.
You create multiple layers that scroll all at another speed (because one layer is bigger then the other)

The scrolling code is quite easy:

1
2
3
for each(var layer:MovieClip in layers){
	layer.x = -((player.x / initwidth) * (layer.width - initwidth));
}

It loops thrue a array with layers and scrolls then using the player position.
You can download the example and view the complete code bellow with a simple player that can walk from left to right and right to left.

The complete code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var initwidth:int = 550;
var keys:Array = new Array();
var layers:Array = [background_layer, foreground_layer, trees_layer];
 
function render(e:Event):void
{
	for each(var layer:MovieClip in layers){
		layer.x = -((player.x / initwidth) * (layer.width - initwidth));
	}
	if(keys[39]){
		player.x < initwidth - player.width ? player.x+=5 : initwidth - player.width; 	} 	if(keys[37]){ 		player.x > 0 + player.width ? player.x-=5 : 0 + player.width;
	}
}
function handleKeyDown(e:KeyboardEvent):void
{
	keys[e.keyCode] = true;
}
function handleKeyUp(e:KeyboardEvent):void
{
	keys[e.keyCode] = false;
}
stage.addEventListener(Event.ENTER_FRAME, render);
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);

Download
The demo:

Parallax scrolling

Categories: Actionscript