Creating a Asteroids Flash Game Part 9: Integrating with the Kongregate API

Chris Moeller Web Creating a Flash Asteroids Game part 10 - Integrating the Kongregate API for high scores

In the last section, we created a way to keep track of the lives and score, as well as created levels, restarting, pausing, and a basic menu GUI to start the game.

In this section, we’ll add the Kongregate API to the game to allow us to keep track of the score through the Kongregate game site.

You can preview the game on Kongregate here:

Tutorial Demo

Adding the Kongregate API access

Kongregate makes it very easy to submit high scores and other values using their API. You can check their website for any updates in the process.

First add a new folder under your ‘src’ folder called ‘APIs’. Next, create a new class inside that folder named ‘’.

package APIs 
	import flash.display.Loader;
	import flash.display.Stage;
	import flash.system.Security;	
	 * ...
	 * @author Chris
	public class KONGREGATE 
		public var stage:Stage;
		// Kongregate API reference
		public static var kongregate:*;	
		public function KONGREGATE(stage1:Stage) 
			stage = stage1;
			// Pull the API path from the FlashVars
			var paramObj:Object = stage.loaderInfo.parameters;
			// The API path. The "shadow" API will load if testing locally.
			var apiPath:String = paramObj.kongregate_api_path ||
			// Allow the API access to this SWF
			// Load the API
			var request:URLRequest = new URLRequest(apiPath);
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
		// This function is called when loading is complete
		private function loadComplete(event:Event):void
			// Save Kongregate API reference
			kongregate =;
			// Connect to the back-end;
			// You can now access the API via:
			// kongregate.user
			// kongregate.scores
			// kongregate.stats
			// etc...
		public static function SubmitScore(score:int):void


Now that we have the class that will be able to send off data to the Kongregate servers, we need to initialize it. So in our ‘’, we need to add


at the top of the file with the rest of the import statements, and into our ‘init’ function:

new KONGREGATE(stage);

Which will leave us with the new


	import flash.display.Loader;
	import flash.display.Sprite;

	public class Main extends Sprite 
		private var game:Game;

		public function Main():void 
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		private function init(e:Event = null):void 
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			//create the game object passing in the swf width and height
			game = new Game(stage.stageWidth, stage.stageHeight);
			new KONGREGATE(stage);

			//add the game bitmap to the screen/ Sprite to make it visible

			//Create the main game loop
			addEventListener(Event.ENTER_FRAME, Run);
			//add keylisteners
			stage.addEventListener(KeyboardEvent.KEY_DOWN, game.KeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, game.KeyUp);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, game.MouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, game.MouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, game.MoveMouse);
		private function Run(e:Event):void

Finally, we have to send our scores to the kongregate servers, whenever we decide to. So in ‘’, in the Update function, when lives are equal to zero, we set game over as true, and we also submit the score at that point:

Excerpt from

				if (asteroid_ship_hit != -1)
					ship.visible = false;
					explosions.push(new Explosion(ship.x, ship.y, 1500,2));
					explosions.push(new Explosion(ship.x, ship.y, 500));
					if (lives > 0)
						ship.ship_death_time = current_time;
						game_over = true;

That should be all you need to be able to get your games displaying the scores using the Kongregate API!

You do need to upload it onto the Kongregate website to be able to see the scores, and after it submits the score, you have to refresh the page to see it on the right hand side, under ‘highscores’ tab.

You can see this version live on Kongregate at:

Where to go from here

To continue making this game more advanced, you could add the space ship that randomly appears and fires at the player from the original game.

Additionally, you can create more varied asteroid shapes, create power-ups that the ship collides with to give the player more powers, add a “shield” for the ship, and even make randomly appearing “worm holes” to transport the player to a random location on screen when entered.

Create more challenges, as well as more rewards to make this asteroids game your own!

Download the source Code and please leave me any comments or feedback you have.

Other Articles in this Series

Bookmark the permalink.


  1. this was very interesting could you email a video how to make the extra things.

  2. It looks like this section of the tutorial is missing.
    Did you forget to upload it?

Leave a Reply

Your email address will not be published. Required fields are marked *