Flash AS3 and PHP File Encoding
A quick tip: (after spending an hour banging my head on the wall):
If you are to have PHP talk to Flash and pass binary data through a URLLoader, keep one thing in mind; The PHP file has to be saved in ANSI encoding. Saving the file in UTF-8 encoding will only give you headaches!
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.
Flash CS3 Components in CS4
After i installed Flash CS4, the first project i wanted to import and "upgrade" from CS3 was of course our homepage, www.locus-delicti.com. But when i did it and hit Ctrl+Enter all kinds of weird stuff happened and items started flashing here and there.
That was a while ago, so now that i had some free time, i decided to give it another go. Among a lot of errors, one stood out:
Error 1034: Type conversion failed. Cannot convert fl.controls.TextField to MovieClip
It seems that CS4 had some problems with my old TextArea component from CS3. The solution was simple but confusing. I just dragged a new TextArea from the Components Panel and chose to "replace existing component". After that everything worked.
The problem is, i never saw in the new features of Flash CS4 that the components have been retouched (backwards-incompatibly)...
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):
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!
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.
AS3: Make a wordlist from a String of words
Working on dreamgrove.org i had to turn a string of space-separated words into a word list, an array containing these words, so that when someone searches for "red dress" i could get results using the String.search() method even when these words are not adjacent to each other (eg. "... her dress was red"). So i wrote a simple function that takes that string and returns an array of the words. Here is the code:
var string:String = "Google's mission is to organize the world's information and make it universally accessible and useful";
function wordlist(str:String):Array{
var wordlist:Array = new Array();
while(str.search(" ") != -1){
wordlist.push(str.substring(0, str.search(" ")));
str = str.substr(str.search(" ")+1);
trace(str);
}
wordlist.push(str);
return wordlist;
}
trace("\nlist: "+wordlist(string));And the output is:
mission is to organize the world's information and make it universally accessible and useful is to organize the world's information and make it universally accessible and useful to organize the world's information and make it universally accessible and useful organize the world's information and make it universally accessible and useful the world's information and make it universally accessible and useful world's information and make it universally accessible and useful information and make it universally accessible and useful and make it universally accessible and useful make it universally accessible and useful it universally accessible and useful universally accessible and useful accessible and useful and useful useful list: Google's,mission,is,to,organize,the,world's,information,and,make,it,universally,accessible,and,useful
AS3 Basic Fractal
(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.