Archive for the ‘Code’ Category

How To Make Your Own Bot for Robot Wars III

Saturday, May 22nd, 2010

The Tournament

Create your own fighting robot in AS3 and watch it battle it out in FlashBrighton’s third Robot Wars tournament. It will be held on Tuesday 25th May 2010 at 19:00 BST at The Werks in Hove. The event will also be streamed live at live.flashbrighton.org.

Submission

If you wish to compete, send your Bot and BotController classes to legojoe [at] gmail.com. The deadline for submissions is Tuesday 25th May 17:00 BST. UPDATE: There will be a one hour coding session before the tournament, so the deadline is now 20:00.

Quick Start

  1. Download the source and add it to your ActionScript 3 (FP10) project.
  2. In org/flashbrighton/as3bots/bots/ create a new class that extends BaseBot.
  3. Create another class that implements IBotController.
  4. Use the code from JoesBot and JoesBotController to get a good idea of where to start.
  5. In Main.as create a new instance of your bot in the addBots() method.
  6. Compile and watch!

(more…)

Searching hierarchical data (without recursion)

Wednesday, March 10th, 2010

Yesterday, I needed to tie in some native menu items in an AIR application, so after a bit of Googling, I came across the AIR docs, which had a description of how to do exactly what I wanted. However, there were 2 problems with the example they had. Firstly, it used a recursive function, which is slow. Secondly, the code didn’t actually work, which is really not helpful.

So, I had to write my own and thought I’d share it here. This kind of function is good for any hierarchical data; tree structures, menus, file systems etc. and wouldn’t take much to adapt. Here’s the code:

public static function findNativeMenuItemByLabel(menu:NativeMenu, label:String):NativeMenuItem
{
	// first create the vars and store the first item
	var currentMenu:NativeMenu = menu;
	var currentIndex:int = 0;
	var indices:Dictionary = new Dictionary(true);
	var menus:Array = [currentMenu];
	indices[currentMenu] = currentIndex;
 
	while (menus.length && currentIndex < currentMenu.numItems)
		{
			currentMenu = menus.pop();
			currentIndex = indices[currentMenu];
		}
	}
 
	return null;
}

This works by storing an item and an index for each item. It then checks the item against the search term (in this case label) and returns it if it has found the item we are looking for. If not it checks whether it has any children. If so, we store the item and its index (note that we increase it by 1 before we store it – this is so when we come back to the menu, we pick up the next item in the list, rather than the one we were last on) and move onto the submenu. If there are no children, we move through the list, checking each in turn, until we have checked them all. At this point, we pull out the item we stored before (the parent menu) and continue working through, until we have checked all the items.

Using this kind of ‘stack’ system is much faster than using recursion, uses less memory and, I think, a little easier to understand.

Hope this helps!

Owen

UTC Date (time is relative)

Friday, February 26th, 2010

Anyone interested in Dates and passing them between different languages, I had an issue with selecting dates in a Flash UI which uploads them to a data base via a webservice and pulls them back again. Some were coming back a day earlier and some not. After some (hours) investigation I tracked down the offending  date to 29 March… Ah, British Summer Time +1 hour. Flash sends the date out as UTC (universal time) so the date is being read on the server as the day minus one hour to compensate if in BST. The default setting for hours is zero so if not set this equals the day before.

The solution to read the date on the server as Locale time, though depending on the circumstance this may only be wise if you can be sure both server and client are in the same time zone. Or in AS to set the hour of the date to anything greater than zero, or substract the timezoneOffset from the minutes.

Though useful if dealing with time zones it can be a little confusing if you just want a date of a particular day. UTC is the not only the date but a point in time as well. The date of that time could be yesterday if your east of Greenwich or tomorrow if west.

var a:Date = new Date(2010,2,29);
trace(“UTC Date: “+a.toUTCString()); //UTC Date: Sun Mar 28 23:00:00 2010 UTC
trace(“Locale Date: “+a.toLocaleDateString()); //Locale Date: Mon Mar 29 2010
trace(“——————–”);
var b:Date = new Date(2010,2,29);
b.minutes -= b.timezoneOffset;
trace(“UTC Date: “+b.toUTCString()); //UTC Date: Mon Mar 29 00:00:00 2010 UTC
trace(“Locale Date: “+b.toLocaleDateString()); //Locale Date: Mon Mar 29 2010
trace(“——————–”);
var c:Date = new Date(2010,2,29,12);
trace(“UTC Date: “+c.toUTCString()); //UTC Date: Mon Mar 29 11:00:00 2010 UTC
trace(“Locale Date: “+c.toLocaleDateString()); //Locale Date: Mon Mar 29 2010

Robotlegs and Signals Talk

Saturday, February 20th, 2010

‘Lo everyone,

Here are the slides from my talk on Tuesday. All the links to the github repos are in the pdf, but I put them below as well for posterity. Enjoy.

http://www.robotlegs.org/

http://github.com/tschneidereit/SwiftSuspenders

http://github.com/robertpenner/as3-signals

http://github.com/richtextformat/RichTextFormat-Classes/tree/master/uk/co/richtextformat/signals/

Owen

RL&Signals <–PDF!

(Also I should mention that the images in the pdf are not neccessarily CC; I didn’t have a huge amount of time to prepare. If I’ve inadvertently used an image of yours, I’ll happily remove/give you a credit.)

AS3Bots – 2nd Tournament Result

Wednesday, November 4th, 2009

bots_t2

Congratulations to Richard Willis – second time winner of FlashBrighton’s Robot Wars Tournament.

Video footage of the night is up on Vimeo .

You can also run the game from here. (Press Space to start and use the arrow keys to control the house bot).

Ideas for the next tournament include:

  • Power-ups, including a radar component and boosters
  • Smaller (or shrinking) arena
  • Health taken off if a bot hits a wall
  • Fix the sticking!

Download the source code if you fancy having a go yourself.

AS3Bots – Designing a Skin for Your Bot

Monday, November 2nd, 2009

The bots can now be skinned with a still image or an animated movie clip. Follow these tips to have your skin ready for the upcoming robot wars tournament.

  • The bots will look best from a top-down point of view.
  • All bots should be designed facing to the right.
  • The ideal shape for a bot is a circle, but a square will also work. The hit area of a bot is a circle and skins will be resized to fit this. bot_resize
  • You can create your design in any graphics application, but once it’s imported into Flash, make sure the registration point is at the centre of the bot.
  • The bot must appear on the first frame of the main timeline in Flash, with the centre of the bot sitting at the top-left of the stage (0, 0). bot_centre

#tweetcoding

Wednesday, March 11th, 2009

A few weeks back, during our generative art workshop, we decided to spice things up a bit by asking Flashbrightoners to produce some generative art in 140 characters!!

Impossible! You cry, but not so, as we introduced everyone to Grant Skinner’s idea of tweetcoding.

The idea behind tweetcoding, is that you are given some “gimme” code, which shortens some common functions into one letter names, which can be found here. You can then use these in your 140 character tweet. Once you have written a masterpiece, you post two tweets. One listing your code, and another containing a title, the #tweetcoding tag, and a link to your original tweet. This then posts it up here for all to see.

During our couple of hours of play, we produced some interesting results.

@atobe made a symmetrical drawing sketcher :


k=stage;w2 = k.stageWidth/2;h2 = k.stageHeight/2;a=mouseX;b=mouseY;z=i%15;ls(0, i%2*0xffffff);dc=g.drawCircle;dc(a,b,z);dc(w2-a,h2-b,z);i++

See it in action here!

@richtextformatmade some interesting mouldy growth :


i=-1;while(++i<99){if(!o[i])o[i]=[r()*255,r()*255];ls(1,1,r());mt(o[i][0],o[i][1]);lt(o[i][0]+=((r()-.5)*8),o[i][1]+=((r()-.5)*8));}

See it in action here!

@retallicka made some interesting psychedelic shapes :


n=m.PI;if(i>99)i=0;if(!i){g.beginFill(0);z=mouseX}if(i%3)lt(i,i+(i*n));if(i%2)g.curveTo(z,z,i/n,2/(i*n));lt((2*i)*n,i/n);i++

See it in action here!

As well as using Grant Skinners library, FlashBrighton’s very own @J4mie made a similar idea into the JavaScript painting library, so people could also have a go in processing. This application can be found here, so have a go yourself! (Although, Jamie stresses that it is currently in very experimental stages!)

Last Night’s Flexo Fun

Wednesday, November 19th, 2008

Well, another successful and packed event at the werks… but enough about the Ruby on Rails coding dojo upstairs, what about FlashBrighton’s flexo?

After a slow start we eventually managed to load in some XML. The next hour was spend trying to work out how to actually access any of the data in it. Then in the last few minutes of the evening we changed the feed from atom to rss2, bound a few properties to a DataGrid, and whammy, our very own slate-blue-flex-skin-a-go-go rss feed reader.

Get the archived flex project here: fbsyndicatedblogrssreader.zip

AS3Bots – Box2D

Saturday, September 6th, 2008

Here’s an update on Phase 3 of the Bots project.

Direct link to the swf

What’s new:

  • Box2D physics engine.
  • Rectangle arena.
  • The bots turn black increasingly when they get hit.
  • The force of impact now plays a part in determining the amount of points lost when a bot gets hit.
  • For now, no damage is taken when bots hit the walls.
  • Energy is no longer gained when hitting another bot.
  • There is a debug bot that can be controlled using the arrow keys.

Those who have created their own bots may need to adjust their logic to fit the new physics rules. The source code is in a new branch in Subversion:

http://as3bots.googlecode.com/svn/branches/box/src

AS3Bots – Create your Bot

Saturday, August 9th, 2008

Here’s a little tutorial on how to create your own bot. I’ll be using Flex Builder on Windows for this tutorial. Flash users will have to skip all the Flex Builder automated goodness and write the code manually.

Step-by-step Tutorial

Here’s the step-by-step guide to getting the game running, using Flex Builder (2 or 3 will work). (Experienced programmers can skip to the quick tutorial below).

  1. Select File – New – ActionScript Project from the menu bar.
  2. Enter Bots for the project name and click Next.
  3. For Main source folder type src and click Finish.
  4. Download the source files and unzip the zip file.
  5. Copy the src folder and overwrite the one in the new project folder.
  6. In the Flex Navigator (on the left) expand the src folder, right-click Main.as and select Set as Default Application.
  7. To test the game, click the Run button (the green button with the white triangle).

When it’s all up and running follow these steps to create your own bot ‘brain’:

  1. In the Flex Navigator expand the src folder and keep expanding until you see the logic folder.
  2. Right-click logic and select New – ActionScript Class.
  3. For the Name type your username followed by BotLogic (e.g. mine would be LegoJoeBotLogic).
  4. Click the Add button next to Interfaces.
  5. Double-click IBotLogic.
  6. Click Finish.

Now you can start coding your own bot logic. Have a look at the other BotLogics in the logics folder for tips on where to start. The file called BotLogic.as contains comments to help you out.

To test out your bot you will need to register it with the game. Here’s how to do it:

  1. In the Flex Navigator find the Game.as file and double-click it.
  2. Scroll down this class to find init() function. (It starts on line 50).
  3. Copy one of the lines that start with the registerBot() functions and edit it to suit your class.
  4. The registerBot() function accepts 3 parameters. The first is a new instance of your class, the second is the name of the bot, and the third is the colour you want your bot to be.
  5. Now save all the files and click the Run button to see your bot in action.

Quick Tutorial

For the seasoned programmers, here’s a very quick guide to creating your own logic:

  1. Create a new ActionScript Project and use src as the main source folder.
  2. Checkout the source files from subversion (http://as3bots.googlecode.com/svn/tags/phase2/src) to the src folder.
  3. Set Main.as as the default application.
  4. Create a new class in the org.flashbrighton.as3bots.logic package ensuring it implements IBotLogic.
  5. Edit the init() method of the Game class and use registerBot() to add your bot.