Create your own fighting robot in AS3 and watch it battle it out in dotBrighton’s fourth Robot Wars tournament. It will be held on Wednesday 9th March 2011 at 19:30 GMT at The Skiff in Brighton. Tickets are available at Eventbrite. If you can’t make it you can still submit your bot to be included in the tournament.
If you wish to compete, send your Bot to legojoe [at] gmail.com. The deadline for submissions is Wednesday 9th March 17:00 GMT. You don’t have to be there to compete, but those attending will get a half hour coding session at The Skiff before the tournament starts.
- Download the source, unzip and add the botlib.swc library to a new ActionScript 3 (FP10) project. There is also code for a Bot that can be used as an example.
- Create a new class with your name that extends Sprite. Ensure your project compiles this class.
- Create another class with your name plus “Bot” as the suffix (e.g. JoeBot) that extends BaseBot. Use the example code to edit the properties.
- Create one more class that implements IBotController (e.g. JoeBotController). Use the example code to come up with your bot’s logic.
- Back in your main class, add the name of your Bot class to the constructor. (This forces the compiler to include your Bot in the swf).
- Compile. The filename of the swf must match your main class, i.e. just your name.
Testing Your Bot
Launch the Test Arena and browse to your swf file.
About Your Bot
Your Robot is based on a circle (viewed from the top) and is essentially blind. It will need to react based on collisions. It can move forwards and backwards and turn a full circle. It won’t run out of fuel, but will lose energy when hit by another bot, or when it hits a wall. The weakest point is the at the back and the strongest is directly at the front. Lose all the energy and your bot will be out of the game.
Creating Your Bot
There is an example Bot included in the source (called Joe). You can use it as a guide to building your own Bot.
The Bots are component-based. You must extend org.flashbrighton.as3bots.bots.BaseBot, which will set up the essential components (the engine, health etc). You can then add an additional ‘upgrade’ component to boost your Bot’s chances of survival.
You must also create a controller, which will handle all the logic and event handling for your bot. This must implement the org.flashbrighton.as3bots.bots.IBotController interface. Once the controller is created, you will need to add it to your bot like so:
The Bot Controller
IBotController is an interface that defines the ‘brain’ of your bot. Each controller must define four methods:
- notify(event:BotEvent) – This gets called whenever your bot hits something. The event will tell you what it hit and where on its body.
- update(data:BotData) – This gets called on every frame. It receives an object that provides information on your bot, such as your current health percentage.
- getEnginePower() – This is called by the Game. You must return your chosen engine power here in order to move. The value must be between -1 and 1 (negative means reverse).
- getSteeringPower() – This is called by the Game. You must return your chosen steering power here in order to turn. The value must be between -1 and 1 (negative means anti-clockwise).
When your bot collides, it will receive an event revealing if it hit a wall or another bot. The event also contains information on where the bot got hit on its body. This value is between -1 and 1. A value of zero means the bot was hit dead on the nose. A positive number means it was hit on its right side, a negative number means it was hit on the left. The image below helps to visualise this:
The strongest point on the bot is the front (a value of 0), whereas the back is its weakest.
You can customise your bot in these ways:
- Size – small, medium or large. This affects speed and strength.
You may choose one upgrade to add to your bot. The choices are currently:
- Armour – Reduces damage by 50%
- Turbo – Increases speed 50%
An example of adding an upgrade:
There is a static method you can call to write text to the screen:
Message.write("My message goes here.");