August 30th, 2011 Actionscript 3, Flixel, Mochi, Tutorial Srdjan 2 Comments



Some 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 Press Space game. Now it is using Flixel game library (version 2.55) and Mochi ads and leaderboard (version 3.9.4 AS3).

Download complete source code of this game (tutorial)

As usual, 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_55 game library installed on your computer as follows:
-> open project in FlashDevelop -> go to Project menu -> Properties -> Classpaths -> Add Classpath.

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 these lines according to the settings of your game!

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


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:

// 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 Mochi Leaderboard in the main menu screen open MenuState.as class 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 class and change these lines:

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



Here you can try the game:

Share

'2 Responses to “Flixel & Mochi game template – update”'
  1. James says:

    Thank you for this! So useful!

  2. dude says:

    Thank you so much!

Leave a Reply


Sultan Solitaire android game

Find us on Facebook