ActionScript3 Category

  • PixelBlitz – It’s time for a fresh start

    PixelBlitz was originally released by Norm Soule as a means to help him speed-up the process of creating a game. He graciously released it into the public domain, and shortly after I joined him in expanding and pushing the feature set upwards and onwards.

    But for various reasons (mostly to do with the geek annoyance that is Real Life™) Norm hasn’t had much chance to update the engine at all, and I was left pretty much on my own with it. Various things about the way it worked internally bugged me. I was still quite green to AS3 when I got involved, and some API design decisions really show because of this.

    Now almost a year later I have a much clearer understanding of how I want it to work. How the API should be structured, how it should sit much better with the Flash IDE. And also how utterly vital documentation and examples are (hello PushButton Engine, I’m looking at you).

    So it’s time for a clean break and a fresh start. I will be discontinuing my involvement with PixelBlitz as it stands today, and focusing entirely on building the new game engine from scratch. As of now I don’t know if that will mean releasing under a new name, to keep Norm’s original creation intact. Or if he agrees perhaps we will literally dump the current codebase and replace it wholesale. Right now that isn’t too important. What is important is that I start carefully planning the new API.

    While I don’t expect any responses to this final part, I’ll throw this out anyway: If you want to get involved, please email me. I would love to not be the only person working on this.

  • Particle Worm Explosion

    I was messing around tonight with the explosion code I wrote for Infinite Ammo, with a squiggly “worm like” behaviour and created the following weirdness. Click anywhere to set off an explosion. Click as much as you like to create total mayhem!

  • Infinite Ammo 4k Source Code Released

    Infinite Ammo 4k

    Well everyone else seems to be blogging about the release of their 4k games, so I’m doing so too :) I managed to get mine finished and submitted on-time. I don’t expect it to win a thing (results are in 3 days time), but I had great fun participating all the same.

    I have created a games page entry for it, and in a slightly unusual move for me I have released the full source code for the game too. You can get it from it’s games page. What you learn from it I have no idea. At the very least there’s a pretty explosion / particle system, and a massively optimised and compressed Tween engine! Or you could just skim down through the code, shaking your head thinking “and he ENJOYED coding this?!” :)

  • How do you extract a Sprite back out of a ByteArray?

    I should probably post this into some AS forums (and will do so later), but I had to get this out there quickly and it’s bugging the hell out of me:

    In short, how do you extract a Sprite from a ByteArray? (or any kind of display object for that matter).

    I can write the object just fine, and read it back into a variable, but I’ll be blown if I can then convert that back into what it was originally.

    Here are my attempts so far:

    var test:Sprite = new Sprite();;,0,64,64);;

    //addChild(test); // to test, works fine

    var b:ByteArray = new ByteArray();

    trace(b.length);    //    0 bytes


    trace(b.length);    //    754 bytes, so our Sprite is definitely in there

    //    Reset the pointer
    b.position = 0;

    trace(b.position);    //    0 as I’d expect

    var newTest:Sprite;
    //newTest = b.readObject() as Sprite;    //    Ends up being null
    //newTest = Sprite(b.readObject());    // Type Coercion failed error
    //addChild(newTest);    //    and kaboom, constant “Parameter child must be non-null.” errors :(

    var take2:Object = b.readObject();
    trace(take2);    // ok take2 contains an object, but how can I get the Sprite out of it?

    trace(b.position);    //    754, so it has definitely read it all

    Ummm someone, please help? :)

  • 4k Game Contest – Some useful tips

    4kOk so after the embarrassment of my first post on this subject, I wanted to present some findings that may help you out on your quest for smaller SWF sizes. More importantly, I know for a fact these all worked for me!

    For those who didn’t read the previous post: I’ve been working on my entry for the 4k Game Competition, and have found the following information during the course of my coding:

    Keep Event Listeners out of the constructor

    I have a whole bunch of init stuff in my constructor (creating game graphics, setting up variables, etc) and at the end I kicked off the game by starting up the main events (keyboard, mouse, etc). But by moving the event listeners to their own function, and calling that function from the constructor, it reduced the size of the SWF.

    If you know the size of something, use it! Avoid references where possible

    When creating a Rectangle I passed in the width and height of a bitmap by reference (bulletBitmap.width). By removing this and passing in the actual known width of the bitmap (6) it saved a massive 20 bytes from the ActionScript code.

    Before: bulletRect = new Rectangle(0, 0, bulletBitmap.width, bulletBitmap.height);

    After: bulletRect = new Rectangle(0, 0, 6, 6);

    Here’s another example where being explicit saves bytes:

    Before: fullRect = stage.getRect(stage);

    After: fullRect = new Rectangle(0, 0, 550, 400);

    This saved us 6 bytes.

    Make Bitmaps Transparent

    I create a bitmap the size of my Stage to hold the game background in. It looks like this:

    gridBitmap = new Bitmap(new BitmapData(550, 400, true, 0x0));

    Now I don’t need this bitmap to be transparent (the 3rd parameter) as it sits at the bottom of the display list. But by setting the transparent parameter to “false” it increased the size by 3 bytes.

    Even default values take-up space

    Object instantiation is expensive in AS3, so it’s best to pre-calc objects you need in your main loops at the start. However who would have thought that the following:

    zeroPoint = new Point(0, 0);

    takes up 1 extra byte than:

    zeroPoint = new Point();

    Even though 0,0 are the defaults for the Point object. Every byte counts!

    Avoid Array references in for loops

    I have a loop in the game that checks through a pool of baddies (standard Array containing a custom Object that extends a Sprite). I started out doing it like this:

    for (var a:int = 0; a < baddiePool.length; a++)

    and then …

    baddiePool[a].value = newValue;

    But by pulling out the array element first at the top of the loop, then referencing that, I saved a massive 33 bytes in total:

    ba = baddiePool[a];
    ba.value = newValue;

    Smaller than a ternary

    Thanks to Kevin Luck for this one :) If you need to run a standard if/else on a value where it could be equal to zero then it can be shortened to:

    x = lx || Math.random() * 550;

    In the code above, x = lx unless lx = 0, in which case set x to Math.random() * 550. This is 3 bytes smaller than using a ternary/if-else equivalent.

    Hopefully the above will help out those also entering the competition. Feel free to comment and add more tips!