Some users asked me to update the Flixel & Mochi game template by implementing the last versions of the Flixel and Mochiads libraries so here is a new version of the template with the game example known as Don’t Press Space 2!
Note: This tutorial is obsolete after the termination of all Mochi services, but it can be still used for a learning purpose.
Setup
The external libraries used in this project are:
- Flixel game library (version 2.55)
- Mochi ads and leaderboard (version 3.9.4 AS3)
The project is developed with the FlashDevelop source code editor. To successfully compile it, you have to add a proper classpath to the AdamAtomic-flixel-2_55 game library installed on your computer as follows:
- open project in FlashDevelop
- go to Project menu
- go to Properties
- go to Classpaths
- click on Add Classpath
Main class
The game name, default level of zoom for the game’s cameras (e.g. gameZoom = 2 : all pixels will be drawn at 2x), Mochi Game ID, game resolution and a movie clip for displaying Mochi ClickAwayAd are defined in Main.as class. Change the next lines according to the settings of your game:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// define game name public static const gameName:String = "Don't press space 2"; // define game zoom public static const gameZoom:int = 2; // define Mochi Game ID public static var strMochiGameID:String = "f2f007e06bbe21d3"; // Substitute this with your Mochi Game ID ! // define Game Resolution public static var strMochiGameRes:String = "600x480"; // Substitute this with a resolution of your game ! // movie clip to show Mochi ClickAwayAd in it - it will be created and loaded in MenuState Class! public static var mcClickAwayAd:MovieClip; |
Menu State class
A Mochi Click Away Ad is defined in create() function of MenuState.as class so here you can change the next highlighted lines if you want to put Click Away Ad on some other position or skip it:
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 |
// display Mochi ClickAwayAd... if (Main.mcClickAwayAd == null){ // create a movie clip to show Mochi ClickAwayAd in it Main.mcClickAwayAd = new MovieClip(); Main.mcClickAwayAd.width = 300; Main.mcClickAwayAd.height = 250; Main.mcClickAwayAd.scaleX = 0.5 * Main.gameZoom; Main.mcClickAwayAd.scaleY = 0.5 * Main.gameZoom; Main.mcClickAwayAd.graphics.beginFill(0xff008800, 0); Main.mcClickAwayAd.graphics.drawRect(0, 0, 300, 250); Main.mcClickAwayAd.graphics.endFill(); Main.mcClickAwayAd.x = (FlxG.width * Main.gameZoom - Main.mcClickAwayAd.width ) / 2 + 60 * Main.gameZoom; Main.mcClickAwayAd.y = (FlxG.height * Main.gameZoom - Main.mcClickAwayAd.height) / 2 + 50 * Main.gameZoom; FlxG.stage.addChild(Main.mcClickAwayAd); // to hide it use: FlxG.stage.removeChild(Main.mcClickAwayAd); // define options for Mochi ClickAwayAd var opts:Object = { }; opts.id = Main.strMochiGameID; // mochi game ID opts.clip = Main.mcClickAwayAd; // attach Mochi ClickAwayAd to this movie clip opts.skip = false; //set this option to TRUE if you want to skip the ClickAwayAd // show Mochi ClickAwayAd MochiAd.showClickAwayAd(opts); } else { // movie clip with Mochi ClickAwayAd in it is already loaded -> display it! FlxG.stage.addChild(Main.mcClickAwayAd); } |
To show a Mochi Leaderboard in the main menu screen change the next lines inside MenuState.as class according to your leaderboard settings:
1 2 3 4 5 6 7 8 |
private function showMochiScore():void { // Substitute these with your MochiAd leaderboard code !!! var o:Object = { n: [4, 2, 2, 9, 14, 10, 7, 12, 10, 5, 14, 5, 2, 4, 2, 15], f: function (i:Number,s:String):String { if (s.length == 16) return s; return this.f(i+1,s + this.n[i].toString(16));}}; var boardID:String = o.f(0, ""); MochiScores.showLeaderboard( { boardID: boardID, onDisplay: displayMochiScore, onClose: closeMochiScore, onError: closeMochiScore } ); } |
Play State class
To send the score to the Mochi Leaderboard when the game is over change the next lines inside PlayState.as class according to your leaderboard settings:
1 2 3 4 5 6 7 8 9 10 |
private function sendMochiScore(Timer:FlxTimer=null):void { // Substitute these with your MochiAd leaderboard code !!! var o:Object = { n: [4, 2, 2, 9, 14, 10, 7, 12, 10, 5, 14, 5, 2, 4, 2, 15], f: function (i:Number,s:String):String { if (s.length == 16) return s; return this.f(i+1,s + this.n[i].toString(16));}}; var boardID:String = o.f(0, ""); MochiScores.showLeaderboard( { boardID: boardID, onClose: closeGame, onError: closeGame, score:FlxG.score } ); ... } |
The game example
Note: After the termination of the Mochi services, Mochi ads and leaderboard is not working anymore, but the game is still playable!
Thank you for this! So useful!
Thank you so much!