...these be from google reader...


Archive for the ‘AS3’ Category

Serializing and deserializing AMF0 Mediaserver2 calls with AS3

Thursday, July 19th, 2007

I’m currently working on a multiplayer game (well, 2 player), a flex app which uses Flash Media Server2.
I use the Command Pattern in the app for “player moves”. I have a IGameCommand interface and a bunch of Command classes which implement it. Users select/create moves (command instances) which they send to the Media Server using AMF0 . The Media Server then validates both moves, and sends them back to the players, at which point the client side flashes execute the commands.
Here the tricky bit was the serializing / deserializing, as AMF0 only handles generic Objects… I didn’t want to have this massive switch statement to determine the type of command, then serialize it… Miraculously I recalled this blog post by Colin Moock, and salvation was at hand! I love AS3.

It turns out that in flash.utils there are a few VERY useful functions…

first, flash.utils.getQualifiedClassName() returns a string like : “net.sakri.commands::KickAss” (I’m not sure why the double colon, I’ve seen that syntax before in php and so). As an argument, you can pass an instance of an object, or the classname. This is perfect for the serializing, as the serialized object can be given a variable which holds the “qualified” name of the class that it was serialized from.

Below is an example implementation inside a Command classs (function getSerializedAMF0Object():Object is defined in IGameCommand interface):

[as]
public function getSerializedAMF0Object():Object{

var obj:Object=new Object();
obj.qualified_class_name=getQualifiedClassName(this);

//rest of props

return obj;
}
[/as]
This is kosher AMF0. The server then pushes such serialized commands to both players, and here is the code which releaved me from the nasty switch statement:


[as]
public function commandFromServer(c:Object):void{

var ClassReference:Class = getDefinitionByName(c.qualified_class_name) as Class;
var command:IGameMoveCommand=new ClassReference();
//pass the rest of class props from the serialized object,
//all of which adhere to the IGameMoveCommand interface

}
[/as]
The reason this rocks so much, is that I can now create as many commands as I like, without having to touch the send/receive, serialize/deserialize code. This method could also be used for “real” serializing, meaning, you could store your data objects as xml or strings or whatever, and know that painless instantiation awaits the stored objects return :)

The Batman Text Effect

Thursday, July 12th, 2007

This is the final posting about my presentation at multimania (sniff sniff), and I’ve saved the best for last… The idea is nothing new, a text, which either comes together from pieces, or explodes into pieces… I made one at nascom for a Belgian radio station called Donna a few years back… Pigs Farting, right up my alley ;)

Good Morning Donna
seems there is still a version online :)

I wrote a flash based “fart text generator”, where I could create letters by adding and removing bubbles, dragging them around and eventually storing an entire alphabet in xml… this was then referenced by the animation, and fart text was born… took a good 5 days to make the “text generator” if memory serves me…

I came up with an automated way to do this (though I must disclaim that someone has probably done this before, as always). The idea is to grab a snapshot (bitmapdata) of a textfield, using a Matrix transformation to shrink it… When the bitmap data is small enough, a “for loop” can grab all the pixels which are not transparent (using getPixel() ) . These are stored in an array, which in turn can be used to place “the pieces”. With the speed increases in flash player 9 this happens acceptably fast. Works for any font (download the source and change the font if you don’t believe me).

here it is (Make sure you have sound on!):


batman text effect preview
The riveting batman text effect!

Click to see the text explode (and sound thunder), right click to view source (or click here

I have a bunch of ideas to use this for… bleeding text, smoking text, burning text… if any of my infinite fan-club members decides to use this technique, be sure to post here about it ;)

MMUG meeting with Ralph Hauwert and Koen De Weggheleire

Friday, July 6th, 2007

So, when your local user group meets up, a bunch of geeks get together and drink wine in posh castles like this:

kasteel van brasschaat

Right? Didn’t think so :D Gotta give it to the organizers….

So, try imagining someone that ‘codes optimized bump map rendering for vm 3d engines’…

With an instinctual sense of schadenfreude, one conjures up images of 2 inch thick glasses with bandaid, nervous twitches and other generally creepy character attributes… Yet, as so many times in the past (with flash devs), I was surprised that :notworthy: Ralph Hauwert :notworthy: was not only “totally normal”, but a very nice guy and a great public speaker! (not forgetting inspirational). I’ve been reading lots, seen demos and even tried some tutorials with papervision3d before. I was enthusiastic, but getting a ‘first hand’ walkthrough the project really got me psyched! No wonder my mailbox is daily filled with the papervision mailinglist :D Can’t wait to get my hands dirty….

It was cool to see him create a Collada file in 3dsMax (oh, and explain wtf collada is in the first place, nothing to do with coconut alcohol), then import it into a flash project and manipulate it… He also spoke about the groups philosophy for creating the best possible api (easy for flash users to grasp), which got a bit thumsup for me. Impressive demos, great info… thanks for coming!

Koen’s presentation went straight for the jugular of image manipulation in as3… It’s tough to make the matrix class sexy speaking material, but Koen was well prepared and I learned a couple of things… First thing I did this morning was to try out the DisplacementMapFilter… I grabbed some old code and put together this magnificent rip off of Koens wavy text effect ;)


displacement filter text effect
displacement filter text effect

again, right click for source, or click here

I used net.sakri.graphics.WaveGraphic to create a (wait for the surprise) a wave graphic, to which I added 60 y blur… It’s the same wave graphic as in the infamous “web2pointOHmyGod Logo Explorer” I then scroll the lil bastard using net.sakri.component.HorizontalBitmapScroller to (again, tough to anticipate this one) scroll a snapshot of the wave graphic… HorizontalBitmapScroller does the “panorama thing”, at first I tried BitmapData.scroll() , but the displacement fun ends real quick like that ;) Then, as Koen had demonstrated (I still can’t believe I never saw it before), I run DisplacementMapFilter on a textfield, and, VOILA!

I just had to add the reflection, but damn… my dual core is going to 60%… resource hog from hell… oh well, no time to optimize (I guess the .swf could be smaller than 800×600), but such is life ;)

anyway… Looking forwards to the next event!

Text Reacting to Sound

Sunday, June 24th, 2007

Right, as an attempt to imprint my name into the retinas and craniums of all who made the mistake of attending my multi-mania presentation, I had a “text effect which reacted to sound” covering the moviescreen… (see a piece of it here). My name, accompanied by my very own pirate smileypiratepirate smiley smileys, bouncing to the wicked sounds of dj Zinc’s “super sharp shooter”. :buttrock:

I later on explained how I used the “split text field” effect (see text effect explorers, split text effect explorer), to react to the wonderful AS3 flash.media.SoundMixer.computeSpectrum() functionality. All the magic happens in the “reactToSound” method (just view source). The code loops one of the returned channels, grabs a readFloat() from the spectrum at an interval defined by the number of letters in my effect, divided by the available bytes (256). I then use this value to “y position” each letter and a few “followers”…


picture of text effect reacting to sound
View the lobotomizing und brainwashing text effect!

click here for source goodness, or right click on the .swf then “view source” :)

The reflection was more or less copied from : Ben Stuckis reflection , I also used a “Easy Button” from the original AS3 samples written by the mighty Senocular, not his greatest contribution to the world of flash ;) , but saved me a few seconds, and, gave me a good reason to give a shoutout to the great man :D I used audacity open source sound editor (the gimp of sound editors) to create the “loops” (lol). If nothing else, I know I have no future in the world of music :D

ConvolutionFilter explorer

Tuesday, June 12th, 2007

Mastering this powerful filter is something I’ve had on my ‘todo list’ since, oh, last october or so when I read Actionscript3 Cookbook . In chapter 10 : “Filters and Transforms” there is example code for generating “embossing, sharpening and Edge Detect” filters using the ConvolutionFilter, as Borat would say, very naaaiice…

Instead of typing in a gazillion “test matrices” I decided to follow the tradition of “flex [fill the blank] explorers”, so, behold,


The Dazzling ConvolutionFilter explorer

again, right click on the app for source…

I didn’t actually have the “moment of AAAH” :ooh: that I was hoping for… I gained a wee-bit of insight, but more playing around is certainly required. If any one of you millions of fanatic sakri.net/blog readers (who no doubt will spend days playing with this 31337 application) happen to come up with interesting Matrices (or insight), don’t hesitate to post responses, or, if you are shy, send me an email from the contact form on my homepage…

On another titilating side note, this was the first thing I built with moxie… nope, I didn’t use any of the new features, but I thought I would sound far far cooler…

tah tah

Flex Text Effect Explorers

Tuesday, June 5th, 2007

As promised, here’s the first set of “samples” and source code from my multi-mania presentation… I started the session with an introduction (or reminder, or sleeping pill, depending on your level of Flexpertese (har har)) on the distinguished topic of “Inbuilt Text effects in Flex”. It does come with a fine selection, so the point is, get to know them well, before you put on your cowboy hat and try to recreate one thanks to not knowing what’s available.

This one covers the basics, be sure to test out the different easing functions and be CERTAIN to dazzle yourself with the sheer awesomeness of the IRIS!!!!11 :ooh:



The Incredible Flex Text Effects Explorer

Swish, eat your heart out… thanks to the TextField.getCharBoundaries(), The “Split Text Effects Explorer” (for the lack of a better marketing term), recreates a bunch of very common text effects, only, the amount of code required is drastically reduced from the day of old, and, the text retains it’s spacing integrity!



The Uncanny Flex Split Text Effects Explorer

Finally, my take on the ever so popular Web2.0 Logo Creator by Alex P. This one actually features some of the other topics of my session, namely, using BitmapData, Bitmaps, the Drawing API and BlendModes for “photoshoppy” text effects… More on those some other day. The reflection was pretty much stolen from : Ben Stucki . It still irks me that I didn’t have the time to add that spinning yellow “beta” star thingy-ma-bob…



The Thunderstriking Web 2.OMG WTF ROTFLOL LOGO Explorer

Either right click the projects for source, or go here. The code is crap, I know, (read the disclaimer in the source), not a fountain of sustainable project building material, but with a keen eye you’ll find the key routines etc.

enjoy! :)

Multi-Mania 2007

Wednesday, May 30th, 2007

I gave a session at multimania last friday (25th may 07) on the fine topic of “Exploring Text Effects in AS3 and Flex2″ (with the marketing name of “Dazzling Text Effects”). There was much rejoicement. My friend Pieter was kind enough to stay awake and take a picture!

speaking at multimania

As you can see, I pretty much rented an empty cinema to pose as a speaker!

Also worthy of note, I forgot my clipper at my inlaws a few centuries ago when I last cut my hair, so, I actually resemble my pirate smiley in the photo… YAARRRRRRR etc. :buttrock:

Apparently I wasn’t all bad, here’s what the guys at thesedays had to say about it :ooh:

I promised I would swiftly provide source code to the files I presented… erm… I will, one at a time, at my own pace, I’ll actually have something to blog about… Like I said, I hacked most of it together only a few nights before the show, so, naturally I would be too embarassed to share such dribble…

Thanks again to Serge, Koen and the other organizers for smoking enough crack to invite me :D