Portrait of the Sleep Deprived aka. locus blog

7Jan/091

Automatic Custom Context Menu in Flash

It's typical, that every time i wanted to add a custom right click menu to my flash apps, i just googled for it and pasted in the usual code. It just occurred to me that these 5-10 lines of independent code don't have to (and shouldn't) be part of the main code of my projects. After all, i always want the same thing in my menus: the name of the project and a link to my homepage. So, to keep my code at a minimum, i converted that context menu into a nice little class. Now, every time i want to add it, all i have to write is:

import com.locus_delicti.utils.LocusMenu;
contextMenu = LocusMenu.buildMenu("kuler Patterns Gadget");

where the String "kuler Patterns Gadget" is the name of my project.

27Dec/080

Dynamic Noise (Sound & Image) in AS3

I've had a script in my "script tests" library for a long time now that used the noise() method of the BitmapData Class on EnterFrame to create a noisy image that somehow resembled the static when we have bad signal on TV. However it just didn't seem complete.
Flash Player 10 came with some major improvements, one of which, is the enhancement to the Sound Class that now allows us to generate sound using actionscript alone.
Check the example below (make sure you hit the "sound" button cause i didn't want the "hisssssS" playing by default on the blog):

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

All the source code is available below in the .fla file. But i'd like to point out online just how easy it is to generate sound in Flash 10 (5 lines!):

var hiss:Sound = new Sound();
hiss.addEventListener(SampleDataEvent.SAMPLE_DATA, hissCallbackHandler);
hiss.play();
function hissCallbackHandler(e:SampleDataEvent):void{
	for(var i:uint=0; i<2048; i++){
		//write random data in the stream
		//random float values from 0.0 to 1.0 are a perfect noise
		e.data.writeFloat(Math.random());
		e.data.writeFloat(Math.random());
	}
}

At the moment, there is an error in the LiveDocs at Adobe that confuses "SampleDataEvent" with "Event". Adobe's example won't work so just stick to the code above.

Download the source file here!

25Dec/082

DateChooser / DateField from Flex to Flash

After searching in vain a whole day for a dateChooser/dateField/calendar component for Flash CS3/CS4 that would work with Actionscript 3 (something like the one there was for AS2) i decided to follow a different route.

Since Flex Builder has exactly what i was looking for i decided to import it. The operation proved to be simpler than i initially thought. So only after half an hour here's what happened.

10Sep/070

AS3 Basic Fractal

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
(note: reduce iterations if it runs slowly)

A very simple fractal, consisting of straight lines, each positioned at the center of the other.
Found lying at the actionscript tests folder. The code looks pretty clean.

Get the source code here.