Flixel & Mochi game template

A tutorial about creating a flash game template by using Flixel game library with Mochi ads and leaderboard. To show how to join these libraries I created a pointless text adventure game called Don’t Press Space.

Note: This tutorial is obsolete after the termination of all Mochi services, but it can be still used for a learning purpose.



The external libraries used in this project are:


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_35 game library installed on your computer as follows:

  1. open project in FlashDevelop
  2. go to Project menu
  3. go to Properties
  4. go to Classpaths
  5. click on Add Classpath




Here is the flowchart of the game template for better understanding how it works:


Main class

To set your Mochi ID and resolution of your game change the next lines in Main.as class:


Menu State class

A Mochi Click Away Ad is defined in create() function of MenuState.as class. If you want to put Click Away Ad on some other position or skip it change the next highlighted lines:


To show a Mochi Leaderboard in the main menu screen change the next lines inside MenuState.as class according to your leaderboard settings:


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:



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!



13 thoughts on “Flixel & Mochi game template

  1. Hi there, awesome tutorial Srdjan! Thank you very much. BTW, why doesn’t the state diagram have End state??? Is this game like perpetual, never ends or what? That’s seems a little bit strange and may confuse some people…

  2. Actually Flash games don’t have an end state. You just return to the menu (and the diagram above shows return to the menu as “Close”). An end state in the case of a Flash game is when user navigates to other page or just closes the browser but this is totally out of the game’s scope.

  3. hello goshki. I have tried using this example, however when i try to test my game, it crashes when i try to show the high-score. i have it set up similarly to your sendMochiScore() function but mine is called when the player dies in level1. when the player dies i see mochi active i the top left corner of the screen. then the game freezes. any hints. thank you. i am using flixel 2.53.

  4. hello srdjan susnic, i have a question about sending the score to mochi. following this example, i am able to view the leaderboard. however i cannot get the score function to work. every time i try to add “score:FlxG.score” the program freezes. if i exclude the score the leaderboard shows up fine. any thoughts?

    1. The example is made for flixel 2.35! That’s why it doesn’t work with flixel 2.53.
      I’m going to prepare a new version for flixel 2.53 as soon as I get some free time.

        1. Well, it’s hard to me to say what is wrong without the source code, but I can say that I’m currently working on a new game using flixel 2.53 and everything works fine for me with Mochi Ads and Leaderboards (I can show and send score without any problems). And I’m using the same code as in this example!

  5. ok. i have a variable _scoreText that is an Flxtext where _scoreText.text=flxg.score.toString();.

    private function showHighScore():void
    var o:Object = { n: [9, 4, 13, 0, 12, 3, 14, 9, 13, 15, 10, 2, 6, 10, 15, 6], 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, Main.strMochiGameID);
    MochiScores.showLeaderboard({boardID: boardID, score: _scoreText});

    i have also tried

    private function showHighScore():void
    var o:Object = { n: [9, 4, 13, 0, 12, 3, 14, 9, 13, 15, 10, 2, 6, 10, 15, 6], 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, Main.strMochiGameID);
    var score:String = o.f(0, _scoreText);
    MochiScores.showLeaderboard({boardID: boardID, score:score});

    showHighScore(); is a function that is called when the player is killed. the game remains in the this state until the enter key is pressed.

    I can see mochi load up in the top left of the screen, but when it tries to access “score” the game freezes. do i need to explain more about what “score” is in another Class is or something.

    i hope this makes sense. thank you.

    1. Well try to submit only FlxG.score :
      MochiScores.showLeaderboard({boardID: boardID, score: FlxG.score});

      And try to compile your project as a realease version (not debug version)!

      1. i have set forceDebugger to false. in my MenuState “Splash1”, the highscore will open, but will freeze the program if I try to close it. in the Playstate: “Level1”, when my player dies, the game will freeze upon trying to open the highscores. I have placed a link to the files here:


        if you have a moment to look at it. thank you.

        1. hi srdjan, the score works only after i reuploaded it to mochi with the updated code. I thought it would work from my computer. It still does not work from flashdevelop, only online so far. thank you.

Leave a Reply

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