Friday, 30 December 2011

Angle-free rotation

In my last post I showed how to aim a gun at a target above or below the gun to fire a ballistic missile, i.e. one moving under gravity. The mathematics was entirely angle and trigonometry free, but I noted that the angle can be calculated, in case it's need to e.g. rotate a gun turret. But it's not actually needed for that: it's possible to generate the rotation matrix to line up an object with a direction, without using angles.

Thursday, 29 December 2011

Shooting up and down

In my last post I suggested that avoiding angles and using algebra made it easier to solve the more general ballistics problem of aiming at a target that's higher or lower. It does, and here's how it does it.

Wednesday, 28 December 2011

Angle-free ballistics

After my post on Monday an obvious question is whether ballistics can be done without angles. The answer is they can, and there are advantages in doing so. This may seem counter-intuitive as finding the angle is a key problem in ballistics, when firing artillery over distance, and requires significant calculations. But dealing with virtual cannons or ballistas we have far more flexibility and can avoid angles altogether.

Tuesday, 27 December 2011

On Optimisation

This is not about any one optimisation approach but about my overall strategy. It might answer some questions readers have, or at least address concerns I've seen raised elsewhere.

Monday, 26 December 2011

Ballistics update

I created a new version of my ballistics demo. The launcher is now fixed in the bottom-middle of the display and drawn so it's clearer where it's firing from. It fires both angles at once, at the same time and speed, so it's easier to see the difference in flight times and shapes, while it also fires on either side. It is embedded below.


Many games need to calculate the direction, or bearing, between game objects. For example the direction of the player or other game character from a weapon, so the game knows  where to aim the weapon.

The natural way to do this is using angles: when describing the direction of something we commonly use an angular measure, often a crude one such as "North West" or "Two O'clock", distinguishing between eight, twelve or more directions. For more precision degrees can be used, which as numbers can have arbitrary precision.

But often angles are a poor way to measure direction. In particular in games it is rarely best to use angles, for two reasons.

Friday, 23 December 2011

Flash Game License

Last week I wrote about wonderfl, a unique resource for Flash developers. But it's not one I use a lot; I've perhaps used it only a few times for experiments and for code I'd like to show working.

By far the most important and useful web site I use is Flash Game License. As it says below its logo it is "The place to buy and sell Flash games.", so it's a marketplace. It's needed because of the structure of the Flash game industry, with many buyers and even more sellers, of games worth so little that they are not worth advertising or heavy promotion.

Thursday, 22 December 2011

Bug Tunnel Defense: Levels

(originally posted 6 June 2011)
The missions in Bug Tunnel Defense started out a lines of code, describing the position and location of every wall, tunnel, entrance and exit. This became unwieldy very fast, and it was clear it would be unusable for the dozens of missions I wanted in the final game.

Wednesday, 21 December 2011

SharedObject file location

One thing that comes up from time to time, that I learned while making Bug Tunnel Defense, is how to control where Flash saves the settings for the game (assuming the game has settings). This is important as the default location depends on the path to the file on the server and the name of the file on the sever. Although this doesn't usually change it can when the file is moved as it's re-uploaded or for security reasons.

Tuesday, 20 December 2011

String to ByteArray

In Bug Tunnel Defense all levels (called missions in-game) are stored as Strings. This includes the built in levels, levels stored on the user's machine that they've created, and levels stored online in GamerSafe's LevelVault and Kongregate's shared content system. Kongregate also uses Strings, so was very straightforward. But LevelVault requires data stored in a ByteArray, so I needed to convert Strings to ByteArray.

Monday, 19 December 2011

More matrices

Last week I wrote about one way to use matrices, passing values to the Matrix constructor to make a transform or scale matrix. Matrices made this way can be passed to functions that need them, minimising the number of lines and temporary variables. This approach is fine for simple uses but breaks down for more complex applications, as the values passed to the constructor require multiple calculation steps, too much to do inline. Fortunately there is another, easier, way.

Saturday, 17 December 2011


As I've used it in two posts I should mention what wonderfl is. It's simply one of the best resources available for Flash developers, in particular ActionScript programmers. It lets developers upload and run ActionScript, for testing or to demonstrate it. Code on the site can be modified, even if you did not write it, by creating a copy (or a 'fork') and working on that. Editing and testing all takes place in the browser, making it often much quicker than setting up a similar project in Adobe Flash CS or a similar environment.

Friday, 16 December 2011

A faster random

Although ActionScript has its own random number function, Math.random(), like most library calls it is not especially fast. But it is easy to replace, to provide a significant speed up, as well as make it easier to do other optimisations.

The basic idea is very simple: instead of calling Math.random()while the game is running call it a number of times at startup to populate a list, then at runtime pull values out of that list. This would not work in all games: it might produce predictable or repetitive outcomes in some cases. But in games where random numbers are used for effect or to provide some variability in a number of ways it is often indistinguishable from Math.random() (which is not truly random anyway).

Thursday, 15 December 2011

One decimal place

This is a small function I wrote to print Numbers to one decimal place. Most user-visible values used in Bug Tunnel Defense were whole numbers and so were stored as int. But a few needed more precision and so were stored as Number. So they did not overflow their TextField they were displayed to one decimal place, using this function.

Wednesday, 14 December 2011

Bug Tunnel Defense release notes

I discussed these in detail on the blog I ran for Bug Tunnel Defense's release. Rather than repost those posts separately I'll summarise them, with links to the originals.

Tuesday, 13 December 2011


The Matrix class in Flash is a two-dimensional matrix, used to draw and display objects on the stage. It is difficult to avoid as it is needed for some calls, especially the draw function of the BitmapData class. The Matrix if something is to be drawn without moving, but generally it's required.

The simplest use is to offset something as it is drawn. I find this is also the most common use, so it's useful to have a quick way to do it. Here's an example of that, drawing a wall block into a layer based on its grid position.

Saturday, 10 December 2011


This is a simple tip about how to do buttons in Flash. You can make them however you like, using code (as I usually do) or in Flash CS's editor. But always make them dynamic.

Friday, 9 December 2011

My 2D Vector class - and why I no longer use it

For Bug Tunnel Defense I created a 2D vector class. The nearest thing built in to Flash is the Vector3D class, but that with four elements is twice as big as I needed, and also uses Number rather than int as I wanted. I wanted something to store 2D grid positions, for game logic, so I created my own class. Then barely used it.

Thursday, 8 December 2011

My TextField class

This is a class I added to replace all instances of TextField in Bug Tunnel Defense, to eliminate half a dozen lines of near identical code after each call of new TextField();

The constructor for TextField for some reason takes no parameters, even though you pretty much have to set some properties for an instance you create in code. I added  parameters for the defaultTextFormat, x and y which I was always setting, as well as an optional Boolean to make it right aligned. For those text items that were fairly dynamic and so could change alignment I also added functions to set the alignment and position at the same time.

Wednesday, 7 December 2011

Bug Tunnel Defense: Names

(originally posted 25 May 2011)

The game had at least four names during development, that roughly trace the history of its design.

Monday, 5 December 2011


Ballistics concerns projectiles such as darts, balls and bullets that move through the air unpowered, after being fired or launched from a gun or by hand. There are many ways to do this but it's common to make two simplifying assumptions.

First it's normal to assume that air resistance doesn't matter, because the projectile is heavy, it is aerodynamic, or the time of flight is so short that air resistance makes no difference. This may be unrealistic but it is good enough for game simulations, and makes the mathematics solvable.

The other assumption is that the target is no higher or lower than the launcher, so they are at the same height. This is more a constraint than a simplifying assumption but it's one that works well for many game types, where e.g. all combatants fight on the same level. It greatly simplifies the mathematics.

Saturday, 3 December 2011

Actionscript communities

Some places where you can discuss ActionScript, especially for game development

Friday, 2 December 2011

The Bitfield class

This is a simple class I made to handle bitfields in ActionScript. It's main purpose is to make it easy to save flags for preference and progress as integers, alongside other integers so simplifying the code for loading and saving which was tricky to debug.

It includes methods to access individual bits and the whole data as an integer for saving. I also added a function to sum the number of bits, to count the number of awards as they were stored in a Bitfield.

Thursday, 1 December 2011

The JIT compiler and constructors

One of the biggest optimisations I did to Bug Tunnel Defense was also the easiest.

ActionScript 3 includes a number of improvements to the language, but perhaps more significantly includes a new runtime, AVM2, which incudes a just in time (JIT) compiler. This speeds up all code significantly but with one notable omission. It doesn't speed up constructors.

Wednesday, 30 November 2011

Less is More

(originally posted 11 June 2011)
While making Bug Tunnel Defense I had many ideas which didn’t make it into the final game. Some didn’t make it off the drawing board but some got as far as being implemented before being removed.

Monday, 28 November 2011

Distance check

Distance calculations are important in many games. One important example is checking distances to compare them to the range of a weapon or weapons; turrets placed by the player or AI enemies that need to decide when to fire. And because there can be many enemies or turrets in game these calculations may need to be done very often, perhaps many times a frame.

Saturday, 26 November 2011

More on Colours

As  I noted two days ago I store all colours as integers, in particular as uint values. These are stored as static members in a class, 'Col', for easy access to them, and also in saved levels created in the level editor (it actually stores only four bits per channel, to match the way the editor works).

Friday, 25 November 2011

Header files

One of the things I had to unlearn when moving from C/C++ to ActionScript was about header files. In C and languages based on C it's common to create header files for constants and definitions used in the program, which are then included in any source file that needs them. Often some or all headers are included in all source files, alongside system headers. The only cost in doing this is compile time, though even this can be mitigated by precompiled headers and incremental compilation, or ignored with a fast enough compiler.

Thursday, 24 November 2011

The Colourise class

This is my replacement for the ColorTransform class. My reason for creating it was as I store colours as RGB integers, such as 0xffffff, 0x000000, but ColorTransform works with separate floating point values. With this class I can easily use RGB colours whenever I need to use a ColorTransform (which is quite a lot).

Wednesday, 23 November 2011

Bug Tunnel Defense: First Screenshots

(originally posted 25 May 2011)

This is one of the earliest screenshots I have of the game. Unfortunately I did not keep many screenshots of early versions. I was mostly developing the engine which either worked or didn’t, while the art was very much placeholder art which I was not interested in capturing for posterity.

Monday, 21 November 2011


Although the x and y of a DisplayObject in ActionScript are accessed as Numbers they are stored internally as twips. Twips are units of one twentieth of a display pixel, small enough to position a DisplayObject with sub-pixel accuracy. But they are not Numbers and so should not be treated as such.

In particular if the position of an object needs to be stored for later use it should be stored somewhere else. Storing it in the built in fields of a DisplayObject is slower and less accurate.

Be especially careful of doing calculations on values stored as twips which can amplify the error, such as subtracting the positions of two DisplayObjects to get their separation or the bearing of one from the other, or using two positions of a moving DisplayObject to calculate its velocity.

Sunday, 20 November 2011

Bug Tunnel Defense: Tunnels

(originally posted 1st June 2011)
From the start I wanted a tower defense game in 3D. Not just one which is rendered in 3D, like Defense Grid, or an isometric view like Doomsday Defense, but one where the gameplay takes place in three dimensions.
Most tower defense games are linear, with enemies travelling along paths to an exit or goal. These games can be very well done, but the gameplay always comes down to overpowering a parade of enemies with upgrades, as any strategy without upgrades is quickly discovered.

Saturday, 19 November 2011

Bug Tunnel Defense information

A screenshot from Bug Tunnel Defense: just thought I should add one in case someone stumbles across this site and wonders what sort of game it is and what it looks like.

The best summary of it the game is the Jay is Games review where you can also play it.

Bug Tunnel Defense

This is the blog for my next game, as well as for my thoughts on Flash. But I cannot start it without mentioning Bug Tunnel Defense, my last game. It can be played here

As well as at many sites across the internet, including some of the most popular. I also have a blog on it here

As Apple are closing down MobileMe next year that blog is going away, and I need a new place to post my games and other things. Hence this blog. I may move posts over before it closes, depending how it goes.