Category Archives: AS3

Posts related to ActionScript 3.0

Webcam controlled motion

I started playing around a little more with my Webcam and extended my previous motion detection example – this time to control the camera of a virtual 3-D space from motion detected in the user’s Webcam. It detects motion area and general direction, albeit with dubious accuracy, but you get the idea.

Due to popular demand, I’ve posted the source code for you lot to play with. It contains the FlashDevelop project file (it’s compiled with the Flex 3 SDK) and my cut-down 3-D engine, Pants3D 🙂

Wii Paint

After getting my Wii remote hooked up with the WiiFlash Server, I knocked up this quick Flash demo which draws the blobs of infrared light the Wii remote detects (it can track up to 4 blobs at once), just like the Wii console’s sensitivity setting dialog does. I just gave each blob a different colour and clear the graphics on pressing the ‘A’ button.

Here’s the code to get it working – requires WiiFlash Server:

import org.wiiflash.Wiimote;
import org.wiiflash.IR;
import org.wiiflash.events.ButtonEvent;
import org.wiiflash.events.WiimoteEvent;
import flash.events.*;

var myWiimote:Wiimote = new Wiimote();
myWiimote.connect();myWiimote.addEventListener(WiimoteEvent.UPDATE, onUpdated);
myWiimote.addEventListener(ButtonEvent.A_PRESS, onAPressed);

function onUpdated (pEvt:WiimoteEvent):void {
var ir:IR = pEvt.target.ir;
var irWidth:Number = 400;
var irHeight:Number = 400;
var irSize:Number = 4;
if (ir.p1) drawCircle(ir.x1*irWidth, ir.y1*irHeight, ir.size1*irSize, 0xff0000);
if (ir.p2) drawCircle(ir.x2*irWidth, ir.y2*irHeight, ir.size2*irSize, 0x00ff00);
if (ir.p3) drawCircle(ir.x3*irWidth, ir.y3*irHeight, ir.size3*irSize, 0x0000ff);
if (ir.p4) drawCircle(ir.x4*irWidth, ir.y4*irHeight, ir.size4*irSize, 0xffff00);
}

function onAPressed (pEvt:ButtonEvent):void {
graphics.clear();
pEvt.target.rumbleTimeout = 50;
}

function drawCircle (x:Number, y:Number, size:Number, colour:Number=0xffffff):void {
graphics.beginFill(colour, .2);
graphics.drawCircle(x,y,size);
}

ActionScript performance tips

Here are a few simple tricks that may help the performance of your code/graphics intensive Flash movies. This is not an exhaustive list, by any means, rather some of the more effective performance tweaks to try out on your projects. There are the usual sensible coding tricks, like using local variables for oft-used references within functions, or planning your code loops carefully and breaking out of loops whenever feasible – but you should be doing these already. I’ll be adding to this post as and when I feel necessary, but will generally avoid the more granular tricks, such as bytecode optimisation. Some of those methods are too complex to explain in simple terms here and generally have a low effort-to-benefit ratio anyway:

Use scrollRect in conjunction with cacheAsBitmap. The cacheAsBitmap parameter of a movieclip can improve performance dramatically, but will cause problems if the render area of the movieclip gets too large (e.g. larger than 2880 in width or height), regardless of whether it is cropped by the viewable area of the Flash Player. The solution is to use scrollRect to constrain the rendering area to desired limits, in this example, the stage width/height:

import flash.geom.Rectangle;
scrollRect = new Rectangle(0, 0, Stage.width, Stage.height);
cacheAsBitmap = true;

Create bitmap snapshots of complex movieclips. Where you may have a movieclip full of layered graphical effects that isn’t animated, you can save a lot of rendering time by creating a snapshot of the movie. Similar to the cacheAsBitmap parameter, but will improve performance further if your movieclip comprises many lines or alphas. The following function shows a quick and dirty way of duplicating a movieclip as a snapshot of the original:

import flash.geom.*;
function createSnapshot (base:MovieClip, mc:MovieClip):MovieClip {
var bounds = mc.getBounds(base);
var bmpWidth = bounds.xMax-bounds.xMin;
var bmpHeight = bounds.yMax-bounds.yMin;
var bmp1 = new BitmapData(bmpWidth, bmpHeight, true, 0x00000000);
var snapshot = base.createEmptyMovieClip("snapshot"+base.getNextHighestDepth(), base.getNextHighestDepth());
snapshot._x = mc._x;
snapshot._y = mc._y;
var rect = new Rectangle(0, 0, bmpWidth, bmpHeight);
var pos = new Point(0, 0);
bmp1.draw(mc, new Matrix(mc._xscale/100, 0, 0, mc._yscale/100, mc._x-bounds.xMin, mc._y-bounds.yMin));
snapshot.attachBitmap(bmp1, 1, "auto", true);
snapshot._x = bounds.xMin;
snapshot._y = bounds.yMin;
mc.unloadMovie();
return snapshot;
}

Use the opaque window mode trick – sparingly! Setting the wmode=opaque HTML parameter of your Flash object can improve rendering performance, but at a potential cost. Not only does it make the rendering order of movieclips and frames more ‘lazy’, but will effect keyboard interaction adversely in some browsers (especially FireFox). Use with caution.

I’ll be updating and tracking back to this post occasionally, so stay tuned…