Showing posts with label code sample. Show all posts
Showing posts with label code sample. Show all posts

Monday, 19 March 2012

Ball-ball collisions

In this post I discussed how to handle collisions in general, at least when one of the objects is a ball for collision purposes. My intention was to follow it up with an example but I never got around to it. Well now I have, and it shows how to turn the theory in that post into practice.

Tuesday, 6 March 2012

Shader from string

I wanted to write a bit more about Flash shaders, but was constrained by being unable to show my workings: I can post screenshots and could even host a working demonstration but I could not upload a shader to Wonderfl (images can be uploaded but not other data). Nor could I easily load one from elsewhere because of Flash's security model: I would need to load it from a server where I could edit the Crossdomain.xml file.


But a search of Wonderfl and a further web search turned up the solution: the data can be embedded in the source as a Base64 string.

Friday, 2 March 2012

Drawing a pie chart

This is something I wrote for the game I'm working on but removed a few days ago as I went in a different direction in the design. As the code is fairly self-contained I thought it might be of interest.

Tuesday, 21 February 2012

More ball-wall collisions

The observant among you might have noticed that the ball in my last test app was only bouncing off the floor not the wall. I've fixed this in a new version of the app, which improves on the previous version in another significant way.


It now bounces off both both walls as well as the floor, using essentially the same code, which is compact enough to reproduce in full:

Monday, 20 February 2012

ball-wall collisions

Collisions between a ball (in two or three dimensions) are perhaps the simplest to code. This is one consequence of the mathematical simplicity of ball physics, as discussed in my last post. As long as certain assumptions are made the code for such collisions is only a couple of very simple lines.

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.

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.

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.

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.

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).

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).