Random Seeds - Ordering Disorder

Randomness is great, that's a fact, it's very useful in many areas of programming, and is especially good to build non-repetitive games. However in some cases, it's useful to be able to reproduce randomness.

Let's say you have a Tetris game or a puzzle that randomizes it's different elements before the game start. It might be a nice feature to offer a "Try again" button, which would allow the player to replay the exact same game, to see if he performs better. Or you may want to have two players play the same game to compete against each other, while still playing a random game.

There are two approach to this, the first and maybe the most straightforward is to log the random somewhere to be able to replay it. This works of course, but it forces you to implement the entire logging process, and it uses memory. That's not efficient.

Continue reading...

March 13, 2008 // ActionScript // Post a comment

AS3 and the Display List, addChildAt magic.

ActionScript 3 introduced the Display List, which is the new version of the old depth system that AS1/2 had, and the Display Objects, which replace the MovieClip, but extending the idea a lot since now there are many types of objects that can be added to the Display List.

There are many advantages to this implementation, you don't HAVE to provide a depth anymore, the Display Object names are optional as well, and it's all more consistent as the only way to add an object to the list is through AddChild/AddChildAt, no more createEmptyMovieClip, attachMovie, etc.

All of this is quite easy to adjust to as it's really nicer and more powerful than AS2 was, but until recently I was stuck with my old habits of using depths. What I mean is that, in AS2, if you did ten times createEmptyMovieClip('name',0); then only one MovieClip was left at the end, as each new MovieClip created was overwriting the previous at depth 0.

I recently wanted to add an object at the bottom of the stack, to display it as a background. So I looked at my options, and since I was still thinking in the "AS2 way", I thought about using swapDepths (now swapChildren) on all the current objects to move them up and free the zero-depth.

But then when I went into the documentation to check if this could be done more efficiently with a new method I didn't know of, I realized that the addChildAt method, if set to an already occupied index, will not overwrite it as it happened before. Instead it will just push all the objects up to make room for your newly added object. This is a really great feature, which allows you to really forget all about depths. swapChildrenAt acts in the very same way, allowing to move things around without having to worry about keeping track of all your used depths.

December 21, 2007 // ActionScript // Post a comment

Heat maps drawing in Flash

Someone was looking for help to draw Heat Maps in Flash this afternoon on #flash and as I found the matter interesting I gave it a shot.

I managed to do it by drawing grey circles with 1-5% opacity to a BitmapData object, which works a lot faster than working with Arrays to sum up all the values. 

After that first pass the class scans the whole BitmapData to find the peak value and then uses that peak to scale all values and enhance the contrast of the image. At the same time, the pixels are colored depending on their value, which creates the "heat" look.

However, with a high resolution and thousands of heat spots, the process is taking a while to complete so I thought I would use this occasion to try and implement a render loop that computes chunks of data on every frame instead of running all at once and freezing the player. And here is the result, although the heat map doesn't match the underlying map.

Continue reading...

December 16, 2007 // ActionScript // Post a comment

ActionScript 3 Language file for GeSHi

As I wanted to post AS3 code with syntax coloring, I researched it a bit and found GeSHi, a PHP syntax highlighter, for which you can create language files quite easily. There is currently no AS3 file available in GeSHi though, so I decided to build one.

For now it's available here, but I hope it will make it into GeSHi's next releases. It's following the FlexBuilder2 colors for the default style, but it is stylable with CSS.

Should you see any missing keyword or anything, please contact me so I can update it.

Edit : I updated this file to v1.0.1, I was so focused on scraping data that I forgot the "this" keyword in the process, that's now fixed.

Download : AS3 Language file

November 30, 2007 // ActionScript, PHP // Post a comment

Throwing errors before a super() statement

Lately I was building a class which takes XML as it's configuration, reads part of it and then calls super() with some parameters.

However I wanted to throw an error if the XML did not contain valid settings, and Flex doesn't let you do that. Upon compilation it returns an error saying 1201: A super statement cannot occur after a this, super, return, or throw statement.

Fortunately, it's possible to overcome this by creating a helper function or class that will throw the error for you, so that's what I did with my ErrorThrower class. It is as simple as it can get, and it fools the compiler alright.

package com.seld.errors 
     public class ErrorThrower 
         public function ErrorThrower(msg:String, id:int = 0):void 
             throw new Error(msg, id); 

Then with that class, instead of doing throw new Exception('foo'); you just do new ErrorThrower(msg); and it will be compiled, error free.

November 27, 2007 // ActionScript // Post a comment