February 23rd, 2011 Actionscript 3, Flixel, Mochi, Tutorial Srdjan 13 Comments



I created a pointless text adventure game called Don’t Press Space where you have to survive as long as you can. The game is made for tutorial purposes to see how to join Flixel game library (version 2.35) with Mochi ads and leaderboard (version 3.9.1 AS3).

Here you can download complete source code of this game! I think it is quite good commented so there is no need for more explanations here.

The project is developed with 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:
-> open project in FlashDevelop -> go to Project menu -> Properties -> Classpaths -> Add Classpath.

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

Flixel Game Template Flowchart

To set Mochi ID and resolution of your game open Main.as and change these lines:

// 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 !



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

Main.mcClickAwayAd = new MovieClip();
			
Main.mcClickAwayAd.width = 300;
Main.mcClickAwayAd.height = 250;
				
Main.mcClickAwayAd.scaleX = .5;
Main.mcClickAwayAd.scaleY = .5;
				
Main.mcClickAwayAd.x = (FlxG.width - 150 ) / 2 + 50;
Main.mcClickAwayAd.y = (FlxG.height - 125 ) / 2 + 30;
				
this.addChild(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);


To show Mochi Leaderboard in the main menu screen open MenuState.as and change these lines:

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 } );
}



To send score to Mochi Leaderboard when game is over open PlayState.as and change these lines:

private function sendMochiScore():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: onQuitGame, onError: onQuitGame, score:FlxG.score } );
}



Here you can try the game:

Share

'13 Responses to “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. goshki says:

    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. oladitan says:

    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?

    • Srdjan says:

      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.

  5. oladitan says:

    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.

    • Srdjan says:

      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)!

      • oladitan says:

        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:

        https://github.com/oladitan/Hover-Over-City

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

        • oladitan says:

          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.

  6. [...] users asked me to make an update of the Flixel & Mochi game template by implementing new versions of Flixel and Mochiads and here is a new updated version of Don't [...]

Leave a Reply


Sultan Solitaire android game

Find us on Facebook