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:
http://www.kongregate.com/games/doomtoo/chrismweb-classic-asteroids
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 ‘KONGREGATE.as’.
Kongregate.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package APIs { import flash.display.Loader; import flash.display.Stage; import flash.events.Event; import flash.net.URLRequest; 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 || "http://www.kongregate.com/flash/API_AS3_Local.swf"; // Allow the API access to this SWF Security.allowDomain(apiPath); // Load the API var request:URLRequest = new URLRequest(apiPath); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); loader.load(request); stage.addChild(loader); } // This function is called when loading is complete private function loadComplete(event:Event):void { // Save Kongregate API reference kongregate = event.target.content; // Connect to the back-end kongregate.services.connect(); // You can now access the API via: // kongregate.services // kongregate.user // kongregate.scores // kongregate.stats // etc... } public static function SubmitScore(score:int):void { kongregate.stats.submit("HighScore",score); } } } |
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 ‘Main.as’, we need to add
1 | import APIs.KONGREGATE; |
at the top of the file with the rest of the import statements, and into our ‘init’ function:
1 | new KONGREGATE(stage); |
Which will leave us with the new Main.as:
Updated Main.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package { import APIs.KONGREGATE; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.net.URLRequest; 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/ Main.as Sprite to make it visible addChild(game.bitmap); //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 { game.Update(); game.Render(); } } } |
Finally, we have to send our scores to the kongregate servers, whenever we decide to. So in ‘Game.as’, 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 Game.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | if (asteroid_ship_hit != -1) { lives--; ship.visible = false; explosions.push(new Explosion(ship.x, ship.y, 1500,2)); explosions.push(new Explosion(ship.x, ship.y, 500)); DestroyAsteroid(asteroid_ship_hit); if (lives > 0) ship.ship_death_time = current_time; else { KONGREGATE.SubmitScore(score); 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:
http://www.kongregate.com/games/doomtoo/chrismweb-classic-asteroids
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
Creating a Asteroids Flash Game Using Blitting
- Part 1: Setting up FlashDevelop and Planning
- Part 2: Creating the Game Class and rendering the background
- Part 3: Creating and rendering the ship
- Part 4: Using Keyboard Controls to Move Around the Ship on screen
- Part 5: Creating and Firing bullets, and making objects wrap around the screen
- Part 6: Creating Asteroids
- Part 7: Detecting Collisions and Creating Particle Explosions
- Part 8: Lives, Scores, Restarting, Pausing, Creating Levels, and a Basic GUI
- Part 9: Integrating with the Kongregate API
- Part 10: Changing Vector Images to Bitmaps


this was very interesting could you email a video how to make the extra things.
It looks like this section of the tutorial is missing.
Did you forget to upload it?
Hi Dal, Yes I did, sorry about that. It was a lot less work than I thought, but I included the source.