...ndid, the somewhat small but happy love child of Nicolás Delfino. A company specialized in interactive websites and rich media applications, knee deep in actionscript and down with jazz...


Posts Tagged ‘games’

Scoreoid – Highscores the easy way

Posted on: September 24th, 2011
Scoreoid

Today I got the chance to finally play with the awesome game platform by Almog Design (www.almogdesign.net) called Scoreoid.

What Scoreoid is:

Scoreoid is a non-restrictive, reliable and easy to use gaming platform designed to handle scoring, leaderboards and game management, including advanced functions for multi-platform games such as platform content awareness to advanced player management. Developed by game developers for game developers.

(Info from www.scoreoid.net)

Why Scoreoid:

The reason I´m thrilled about Scoreoid is simply because it lets you cut time on an otherwise potentially time exhaustive part of game development, scores.

With that said I thought I´d write a basic tutorial.

Using Scoreoid:

Getting started with Scoreoid is a breeze, all you really need to do is to sign up (www.scoreoid.com/register) and to add a game, the rest is taken cared of by Scoreoid!

To create a game simply navigate to the dashboard and click on “Add new game”. There´s some basic data you need to enter, nothing weird so I leave that up to you.

There´s actually tons of stuff you can do here, and I do recommend you to check it out, but to get to get started on this tutorial you´ll need 2 things:

An API-key

found under your Account Info.

A Game-ID

found under games and specific for the game you´re working on.

Scoreoid + Flash:

Here´s where the magic happens, no downloads – no nothing.
From here on I´ll show you just the absolute basics needed to get you started on a real world situation.

Please note that his example doesn´t necessary follow best practices and how you end up using this is up to you. Also note that I´m writing this inside a document class.

The Document class (Main.as)


package
{
	//SCOREOID - www.scoreoid.net | www.scoreoid.com

	import flash.display.MovieClip;
	import flash.net.URLVariables;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLLoader;
	import flash.events.Event;

	public class Main extends MovieClip
	{
		//setup:
		private var url:String = "https://www.scoreoid.com/api/getGameField";
		private var request:URLRequest;
		private var requestVars:URLVariables;

		private var apiKey:String = "enter your API-key";
		private var gameId:String = "enter your game-ID";
		private var responseType:String = "XML";

		private var ulGetScore:URLLoader = new URLLoader();
		private var ulSendScore:URLLoader = new URLLoader();

		public function Main()
		{
			/*
			Send a score to Scoreoid containing the name of the user and his or hers score */
			sendScore("Player name", "player score e.g 8278");	

			/*
			Show the score on stage
			*/
			getScores();
		}

The sendScore function

This simple function recieves two values passed in as strings: user and score.

We start by pointing the url variable to the appropiate API-call, in this case the one called “createScore” and continue by packing a request handler with data.

The data is contained by an URLVariable we call requestVars which stores the fundamental elements needed to talk to Scoreoid (API-key / game-ID etc…)

Before sending the request we make sure to pass in the two crucial variables needed (score and user, both strings).


		private function sendScore(user:String, score:String):void
		{
			url = "https://www.scoreoid.com/api/createScore";
			request = new URLRequest(url);
			requestVars = new URLVariables();
			request.data = requestVars;
			requestVars.api_key = apiKey;
			requestVars.game_id = gameId;
			requestVars.response = responseType;

			/* send score variables: */
			requestVars.score = score;
			requestVars.username = user;

			request.method = URLRequestMethod.POST;
			ulSendScore.dataFormat = URLLoaderDataFormat.TEXT;
			ulSendScore.addEventListener(Event.COMPLETE, sendScoreCompleteHandler);
			ulSendScore.load(request);
		}

Listen for a respond

By casting the data send back from Scoreoid as xml we´re able to check against a string “The score has been saved” to assure that the score we just send in fact got processed and saved by Scoreoid.


		//Send score complete handler:
		private function sendScoreCompleteHandler(e:Event):void
		{
			//sending scores to scoreoid...
			var xml:XML = new XML(e.target.data);
			if (xml == "The score has been saved")
			{
				trace("The score has been saved.")
			}
		}

Get scores – using it all

This function looks a lot like the sendScore function and would definitly need some cleanup considering the main difference lies in the url (getScores) and the score variables.

The get score variables:

Again, there´s plenty more to check out but to receive the 4 highest scores we use “limit” to limit the result, “order_by” to order the result by score values (“name” would give you a list ordered in alphabetical order) and lastly “dsc” to get the highest score first.


		private function getScores():void
		{
			url = "https://www.scoreoid.com/api/getScores";
			request = new URLRequest(url);
			requestVars = new URLVariables();
			request.data = requestVars;
			requestVars.api_key = apiKey;
			requestVars.game_id = gameId;
			requestVars.response = responseType;

			/* get score variables: */
			requestVars.limit = 4;
			requestVars.order_by = "score";
			requestVars.order = "dsc";

			request.method = URLRequestMethod.POST;
			ulGetScore.dataFormat = URLLoaderDataFormat.TEXT;
			ulGetScore.addEventListener(Event.COMPLETE, getScoreCompleteHandler);
			ulGetScore.load(request);

		}

Putting it out there!

Inside our handler we cast the Scoreoid respond once again as xml and loop through each player object to put a movieclip on stage.

The movieclip we´re using lies inside the Flash IDE library exported for actionscript with the linkage name “PlayerResult”.

PlayerResult:

Contains two dynamic textfields: “name_t” for names and “score_t” for scores.

Scoreoid values:

To show something in our textfields we use “player.@username” and “player.score.@score”.


		//Get score complete handler:
		private function getScoreCompleteHandler(e:Event):void
		{
			var scoreXML:XML = new XML(e.target.data);
			var playerResult:MovieClip;
			var pX:Number = 86;
			var pY:Number = 116;

			for each (var player:Object in scoreXML.player)
			{
				//show results on stage:
				playerResult = new PlayerResult();
				playerResult.x = pX;
				playerResult.y = pY;

				playerResult.name_t.text = player.@username;
				playerResult.score_t.text = player.score.@score;

				addChild(playerResult);

				pY += playerResult.height -1;

			}
		}
	}
}

Presto!

That´s all there is to it really, again you could propably optimize this to half the size but that´s not really the point here.

This is the end result btw:

So download the fla if you want and get your ass over to www.scoreoid.net!

Cheers!
/Nicolás