Showing MochiAds leaderboards in any Flash movie or web page

Ok, so you created your awesome game, embedded MochiAds leaderboards and want to publish high scores in your homepage, in another Flash movie, or wherever you want, just outside the game.

Just think about Kongregate, that shows top scores on the right of the game you are playing.

That’s what we are going to do.

Information stored into MochiAds leaderboards

Every player who enters the leaderboard is saved with its name, the score (obviously), the ISO 3166-1 alpha-2 Country code and the timestamp of his score.

ISO 3166-1 alpha-2 country codes are two-letter country codes in the ISO 3166-1 standard to represent countries and dependent territories.

They are published by the International Organization for Standardization (ISO) as part of its ISO 3166 standard.

Read this page for further information.

The timestamp is the number of millisecond passed from January 1st, 1970

There are other information you can get from MochiAds leaderboards such as the number of entries for each leaderboard, but in this example I am only showing an all-time top 10 chart, so I already have enough information.

Refer to MochiAds Developer Support for the full list of options.

Flash Vs Php

If you just want to display your top 10 in a Flash movie, then you are lucky… MochiAds proviedes an API that does almost all the job.

Problems come when you want to show results in an HTML page.

There isn’t any API to do it and there won’t be any in the future, as soon as I know.

Too good I am a PROgrammer and I can arrange it anyway.

Let’s say I want to display the Top 10 scores for Summer Couples.

Flash Top 10

This is the movie with the name of the dynamic text instances I created

And this is the commented actionscript:

// connecting to mochi services the id below is the id of your game
mochi.MochiServices.connect("summer couples id");
// setting the board id - the id below is the one of your leaderboard and NOT the one of your game
mochi.MochiScores.setBoardID("summer couples board id");
// sending the request for leaderboard datas
// "display table" is the funcion to be called once the request is done
mochi.MochiScores.requestList(this, "display_table");
// ... and here it is the function
function display_table(leaderboard:Object) {
	// changing the "Loading..." text 
	messagetxt.text = "Top 10";
	// creating a variable called array_with_leaderboard that will store all rows of the ALL TIME leaderboard
	// change alltime with:
	// daily — scores for the daily table
	// weekly — scores for the weekly table
	// monthly — guess what?
	array_with_leaderboard = leaderboard.scores.alltime.rows;
	// scanning through top 10
	for (x=0; x<10; x++) {
		// updadint place field
		place.text += (x+1)+"\n";
		// updating points field
		points.text += array_with_leaderboard[x][0]+"\n";
		// updating names field
		names.text += array_with_leaderboard[x][1]+"\n";
		// updating country field
		country.text += array_with_leaderboard[x][2]+"\n";
		// updating timestamp field
		timestamp.text += array_with_leaderboard[x][3]+"\n";
	// sending the variables to /downloads/showmochitable.php page with POST method and display it on "mochiframe" iframe
	// you don't need it if you only want to display values in flash
	getURL("/downloads/showmochitable.php", "mochiframe", "POST");

And here it is my chart for Summer Couples

I did not convert the timestamp and the Coutry code because it's not the aim of this tutorial. Feel free to do it for me and I'll publish your version.

I also did not include any check for errors, so if you see a lot of "undefined", that's because at the moment MochiAds is suffering of some technical problem and cannot display leaderboards.

But when leaderboards work, the script works too.

Remove line 33 if you don't want to show leaderboards in HTML.

HTML (with Php) Top 10

The key of HTML top 10 is in the getURL at line 33.

Now some of you will say it would be better to use something like sendAndLoad but I am going to use getURL for two reasons:

1) In this example I am going to display top 10 in a table, but I would want to do it with an alert called by JavaScript (or any other JS function)

2) I don't have to store anything in a database, just processing POST results

The downside is I am getting a CSV string and not an array... this means sometimes (in example if the name of the player has a comma) I can get wrong result.

The perfect solution would be parsing results in JSON format and post them with getURL (anyone willing do do it?)

Anyway, you can just hide the previous Flash movie (or make it 1x1 pixels sized) and have your results in an iframe with this Php script:

	$html .= "".($x+1)."";
	$html .= "".$scores[$count]."";
	$html .= "".$scores[$count+1]."";
	// showing a flag according to Country code
	$html .= "";
	// converting timestamp in readable format
	$html .= "".date("M d Y",round($scores[$count+3]/1000))."";
	$html .= "";



That outputs this page:

This page too will work only after the leaderboard is correctly displayed in the Flash movie.

No check for errors in this page too, but you are free to improve it.

I used some free flag icons you can find on famfamfam and called with the proper Country code, so the italian flag is it.gif, the french one is fr.gif and so on.

Timestamp is converted using Php date() function, but remember to divide the original timestamp by 1,000 because the original one is in milliseconds while Php accepts timestamps in seconds.

The first thing I'll do now, is enabling leaderboard browsing for my games... if everything goes ok I would like to ask MochiAds developers to share their leaderboard IDs in order to have a lot of games with highscores on

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers