-- --- .-. ... . -.-. --- -.. . - .-- .. - .... .- .. .-. .- -. -.. .--- .- ...- .- ... -.-. .-. .. .--. -
comments (6) Views: 6,866
The first thing I needed to do was get the Morse alphabet, and serialize it. Morse code is simple, consisting of a series of long and short sounds, represented visually by a dot ( . ), and a dash ( - ). I created an object and used the letters and numbers of the alphabet as keys. The values of those keys were the dots and dashes that made up each character.
Then I created a simple input field for the text users wanted converted. Next I found 2 sound clips over at SoundJay.com that I felt best represented a dot and a dash. To keep things organized I created another object to store references to the sound clips. I used a dot and dash as the keys in this object, you'll see why in a minute. Using AIR we then load them into memory as a Sound object, like so:
The next part is pretty straightforward. I grab the contents of the input field, allowing only letters and numbers. I then loop over the length of the string, matching the character with a key in the morse code object. Because I'm only working with two sound files, each character needs to be broken down to it's dots/dashes, so I store each individual dot/dash of the matching string into an array index for efficient looping later on.
As an extra touch, I decided to add a handy little output window so that users could see the resulting morse code tied to each individual letter. This app was a lot of fun to build and I hope you learn something from it.
If this article was interesting, or helpful, or even wrong, please consider leaving a comment, or buying something from my wishlist. It's appreciated!