Many of you may of seen Pixars recent movie UP. In the movie the main character manages to take flight in his house by attaching dozens of inflatable balloons to the roof. Seems a bit far fetched don’t you think? Obviously the guys at Pixar aren’t too concerned with the laws of physics.
Well here at fatlinesofcode we don’t mind if cartoons break the laws of physcis either. But we do love to test them.
I have built a fun prototype game in Box2D to test this house ballooning theory. The house is a box with multiple balls attached via distance joints. Each of the balls has an anti-gravitational force applied to it which provides lift to the house. To pilot this balloon the UP arrow key is used to simulate adding more hot air to the balloons. Each time you press it an upward force is applied to each balloon thus providing more lift to the house. Additionally the LEFT and RIGHT arrow keys can be used to apply left and right forces to the house. Combining these controls you can achieve steady flight. Check out the demo and try manoeuvring your balloon through the various obstacles.
Entries Tagged 'actionscript' ↓
Box2D Ballooning
10th October 2009 — actionscript
Pixelated video effect [Part II]
13th July 2009 — actionscript

I few weeks ago I posted a pixelation effect. I have since optimised the code and also added in support for webcam input.
Checkout the demo and source code below.
Here is an explanation of the technique.
First get the bitmapData source from the video input. Then divide the data into a grid of new bitmaps using a double for-loop. Store each one these in a two dimensional array.
private function createBitmapGrid():void {
var source:BitmapData = getVideoSource();
var _w:Number=source.width;
var _h:Number=source.height;
var rows:Number=source.height/_divideGridBy; // cols
var cols:Number=source.width/_divideGridBy; // rows
var cellWidth:Number = _w/cols-_cellpadding;
var cellHeight:Number = _h/rows-_cellpadding
for (var i:uint=0; i<cols; i++) {
for (var j:uint=0; j<rows; j++) {
var tempBitmapData:BitmapData=new BitmapData(cellWidth,cellHeight, true, 0x000000);
var tempBitmap:Bitmap =new Bitmap(tempBitmapData);
tempBitmap.x=i*_w/cols;
tempBitmap.y=j*_h/rows;
if(! _particleArray[i])
_particleArray[i] = {};
_particleArray[i][j] = tempBitmap;
_holder.addChild(tempBitmap);
}
}
}
Then add an enterframe listener to update the all the bitmap cells in the grid. Add a double for-loop to go through each cell. Use the copyPixel function to copy a rectangular portion of the source image to each cell. After you copy the source data, find the average brightness of the cell and use it as a variable to adjust the z value of the bitmap cell.
this.addEventListener(Event.ENTER_FRAME, updateBitmapGrid);
private function updateBitmapGrid(e:Event):void {
var source:BitmapData = getVideoSource();
var _w:Number=source.width;
var _h:Number=source.height;
var rows:Number=source.height/_divideGridBy; // cols
var cols:Number=source.width/_divideGridBy; // rows
var cellWidth:Number = _w/cols-_cellpadding;
var cellHeight:Number = _h/rows-_cellpadding
for (var i:uint=0; i<cols; i++) {
for (var j:uint=0; j<rows; j++) {
var tempBitmap:Bitmap = null;
if (_particleArray[i]) {
if(_particleArray[i][j]){
tempBitmap=_particleArray[i][j];
}
}
if(tempBitmap != null){
var p:Point = new Point(0, 0);
var rect:Rectangle = new Rectangle(tempBitmap.x+_cellpadding, tempBitmap.y+_cellpadding, cellWidth, cellHeight);
tempBitmap.bitmapData.copyPixels(source, rect, p);
var color:Number=getAverageColour(tempBitmap.bitmapData);
var brightness:Number=getBrightness(getAverageColour(tempBitmap.bitmapData));
if(_moveZ)
tempBitmap.z = 255 - (brightness * 1);
}
}
}
}
private function getAverageColour( source:BitmapData ):Number {
var red:Number=0;
var green:Number=0;
var blue:Number=0;
var count:Number=0;
var pixel:Number;
for (var x:Number = 0; x < source.width; x++) {
for (var y:Number = 0; y < source.height; y++) {
pixel=source.getPixel(x,y);
red+=pixel>>16&0xFF;
green+=pixel>>8&0xFF;
blue+=pixel&0xFF;
count++;
}
}
red/=count;
green/=count;
blue/=count;
return red << 16 | green << 8 | blue;
}
private function getBrightness(colour:Number):Number {
var R:Number=0;
var G:Number=0;
var B:Number=0;
R+=colour>>16&0xFF;
G+=colour>>8&0xFF;
B+=colour&0xFF;
var br:Number=Math.sqrt(R*R*.241+G*G*.691+B*B*.068);
return br;
}
Pixelated video effect
21st May 2009 — actionscript
I have been experimenting more and more with bitmapdata recently. Checkout this pixelation effect I have created for flash video.
Here is how it works. There is an onEnterFrame function which first creates a new Bitmap object from the video source data. A grid of smaller bitmaps is then created from this large bitmap. Each one of the smaller bitmaps is analysied for it’s average color and brightness. The z property (flash 10) is then set to the inverse of brightness. The resulting effect is that for each one of the grid blocks the block moves in and out as the brightness changes. Lighter blocks come forward and darker blocks move back.
Flash is magic. Now watch it and freak out.
Apologies to anyone with a slower machine, this swf is fairly cpu intensive. I would also recommend that you install the very latest flash player(10,0,22,87 ) to ensure you get the best framerate possible.
Runtime pixel snapping
12th May 2009 — actionscript
Lets set the scene: your lazy. You got a sweet a design in photoshop and now you need to bring it to life in flash. Now remember your lazy so your going to use flash’s PSD importer to import the layers into flash. What a timer saver. Flash has has made movieclips and text layers from the photoshop layers, nice. You hookup some functions and publish the file. But something is wrong, some the text looks a little fuzzy and some of the lines are little blurred. You jump back to the fla and check through some of the movieclips. Bah, flash has placed some clips in-between pixels. You’ve even got the ‘Snap to Pixels’ feature turned on but flash has ignored when doing the import.
This has happened to me countless times and often on a big flash project it can be real drag digging through movieclips trying to find the elusive off-pixel clip. It would great I thought if I had a little pixel pixie to go through my FLA and fix up all the movieclips and textfields. Well I couldn’t find any pixies or elfs that where willing to work for me so instead I wrote recursive function to do it at runtime.
This script takes in one argument, the base movieclip it then goes through every child of that movieclip and rounds the x and y position to the nearest pixel. So that every stage instance is rendered on a whole pixel. If any of these clips have children if goes through them and rounds their position and so on and so on recursively until it does each movieclip, sprite or textfield in the swf.
roundChildren(this)
function roundChildren(base:DisplayObjectContainer):void {
for (var i:int=0; i&lt;base.numChildren-1; i++) {
var m:DisplayObject=base.getChildAt(i) as DisplayObject;
var p:DisplayObjectContainer=base.getChildAt(i) as DisplayObjectContainer;
if (m) {
m.x=Math.round(m.x);
m.y=Math.round(m.y);
if(p){
if (p.numChildren&gt;0) {
roundChildren(p);
}
}
}
}
}
Checkout the example below to see the script in action. The pixel snapping does not effect dynamic text because of the way flash anti-aliases it. But you can see a big improvement for the static text and vector lines placed inside a movieclip.
SWFAddress 2.2 ♥ swfobject 2.0
2nd April 2009 — actionscript
SWFAddress is the amazingly easy to use solution for deep linking in flash. Once setup its simply a case of calling SWFAddress.setValue("myfolder") to change the browser location bar and using SWFAddress.getValue()to get the current url when SWFAddressEvent.CHANGE is triggered.
In fact SWFAddress is so simple to use that when something goes wrong it hard to find the problem because there are no options to change. The difficulty I ran into recently was when I upgraded to the latest version 2.2 For an inexplicable reason, swfaddress’s SWFAddressEvent.CHANGE would no longer trigger.
I finally isolated the issue to how you include the swfaddress & swfobject javascripts in your html. The order of includes is extremely important. Swfobject must be included before swfaddress, if you using swffit add this after swfaddress. Also you must add a flash id to swfobject’s attributes for embedding. Check out the example below for for SWFAddress 2.2, SWFObject 2 and swffit harmony.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript" src="swfaddress.js"></script>
<script type="text/javascript" src="swffit.js"></script>
<script type="text/javascript">
var flashvars = {};
var params = {};
var attributes = {id:'mainswf'};
swfobject.embedSWF("main.swf", "container", "100%", "100%", "9.0.115","expressInstall.swf", flashvars, params, attributes);
swffit.fit("mainswf", 960, 580);
</script>
</head>
<body>
<div id="container">
</div>
</body>
</html>
Debugging SWFs within the browser
28th March 2009 — actionscript
If your anything like me use you use the trace statement feverishly when developing a flash project, the trace output is the available within the flash IDE and debugging is a straightfoward process. The problem I have found with using trace for debugging is when you move the project into a browser environment. I frequently find that the swf functions slightly different or there are bugs which cause the swf to silently die. There is however a great solution for viewing the trace statements from the browser.
First you’ll need to download and install flash debugger flash player 10 debugger.
Create a file named “mm.cfg” ( if it does not exist) in one of the following locations:
- Windows;
C:Documents and Settingsusernamemm.cfg - OSX;
/Library/Application Support/Macromedia/mm.cfg - Linux;
home/username/mm.cfg
Open the newly created mm.cfg file in a text editor and add the following text:
ErrorReportingEnable=1
TraceOutputFileEnable=1
Reboot your machine.
Browser debugging should be now enabled. All trace statements from any swf will be outputted to the file /Users/[username]/Library/Preferences/Macromedia/Flash Player/Logs/flashlog.txt
Open a flash site, then look at the file and you’ll see all the traces coming from the swf.
Personally I find opening this file each time annoying so I use the tail -f command from the terminal so I can follow the file and see live trace outputs.
tail -f /Users/phil/Library/Preferences/Macromedia/Flash Player/Logs/flashlog.txt
Water droplet effect
20th November 2008 — actionscript
This is an example of how to use the actionscript displacement filter and bitmapData to create a sliding water droplet effect.
Flash 3D models
16th September 2008 — actionscript
I’ve been playing around with latest version of Papervision. I found this great tutorial on how to load Collada mesh models. Here is a little swf I mashed up. It uses 2 collada models but groups them together as one object with actionscript. Tweener is used to control the animation and mouse events. You view the AS source and DAE model files here.
Paperskate 3D
6th September 2008 — actionscript
(flash + skateboarding) * 3D = awesomeness
Checkout this awesome little flash game built on papervision 3D. Its a very simple idea but pretty effective, lets hope he keeps the development going and takes it to the next stage. If I was this dude, I’d try and market the idea to Tech Deck.
Portfolio idea
16th April 2008 — actionscript
Here is an idea I had for a portfolio. It’s a 3D cube built in papervision to view the websites screenshots. The result was a bit naff so I never developed it further. It was a fun use of Papervision3D and as3 though. Papervision is surprisingly easy. Source is here for anyone that wants it.




