Super Simple Script Snippets – Standalone Leaderboard System, and More

Offender 1 is available for purchase on the app store. Because the submission process was so successful, and it’s a platform just waiting for more Tetrageddon, if taken the time off Offender 2 and am porting Haxed By Megahurtz to iOS.
It’s nearly complete, and going well. I’m excited to see it working on mobile.

The game (Haxed By Megahurtz) was developed for the Nintendo Wii. It took off like wild fire after a Jay Is Games review.
It was a lot of fun to see it spread the way it did, and the reactions people had to it. It was very well received, to say the least.

So, as I port it to mobile, I’ll also be posting some source code bits, and pieces. They’re fun, and may be helpful to others Googling for help, etc…
You can find more in the Open Source Directory. Click here.

The best of which is a standalone (non online) leaderboard system.

Sometimes you find it necessary, and this is a functioning model that I’ve successfully used. Another fun alternative is to use Twitter (or other social sharing methods) as your leaderboards. Just let your players tweet a message from inside the game – containing their score value, plus a link to the game. Of course they can cheat and manually change the score, but it has served as a fun marketing gimmick…

At any rate, this uses shared object, so it’s stored on the player’s machine. If the shared object doesn’t already exist it is created, and populated with an initial set of scores, and names. New name, and score, (player’s) is added to it later.

CODE (CLICK TO DOWNLOAD SOURCE ):

———–


/**
STANDALONE NON-NETWORK SCOREBOARD
SCORE SYSTEM
**/

//This would be your current player's name, and score
var plyr_name:String = "PLYR";//your player name
var score:Number = 2650;//his score

//This is your score shared object, and arrays that get populated from it...
var so_haxxx:SharedObject = SharedObject.getLocal("/"); //shared object for the high scores, stored on the player's machine
var arr_scores:Array = new Array(); //array to contain the scores (saved to, or populated from, shared object)
var arr_names:Array = new Array(); //array with player names associated with the scores (saved to, or populated from, shared object)

//POPULATE SCORES FOR FIRST RUN - IF ALREADY DEFINED SET VALUES
//If this is the first time they are playing populate (save) the scores, and names, array
if(so_haxxx.data.scores == undefined){
//trace("called for first run");
arr_scores = [3300, 3000, 2700, 2600, 2500, 2000, 1800, 1600, 1400, 1000];
arr_names = ["MIFF", "KAWI", "COFA", "EKAW", "MOJA", "BIKA", "ISUX", "RAVA", "IGOT", "COMU"];
//
so_haxxx.data.scores = arr_scores;
so_haxxx.data.names = arr_names;
so_haxxx.flush();
}else{
//If not, then populate both arrays with current scores, and names
arr_scores = so_haxxx.data.scores;
arr_names = so_haxxx.data.names;
}

//SUBMIT SCORE ...
//A very basic script that checks if the score you are saving is greater than any previous scores.
//If it is it saves it according to it's value on the board…
for (var i:Number = 0; i < arr_scores .length; ++i){ trace(arr_scores[i]); if(score>=arr_scores[i]){
trace(score + " is greater than " + arr_scores[i]);
//push new values
arr_scores[i] = score;
arr_names[i] = plyr_name;
//update SO's
so_haxxx.data.scores = arr_scores;
so_haxxx.data.names = arr_names;
so_haxxx.flush();
//break out of loop
i = arr_scores.length;
}
}

[swfobj src=”http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/score_system_standalone.swf” height=”456″ width=”608″]
———–

Aside from that, which I think will probably be the most useful to a googler looking for a solution, there are also these fun bits…

Cartoon Circles (DOWNLOAD SOURCE )
A coded background graphic.
[swfobj src=”http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/cartoon_circles.swf” height=”456″ width=”608″]

———–

Mouse Pan Perspective Scrolling (Background Follows A Sprite or Mouse)
(DOWNLOAD SOURCE )
[swfobj src=”http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/mouse_pan_perspective_scroll.swf” height=”456″ width=”608″]

Code:

/*
Pan top, and bottom border, acording to a movieclip’s position.
Make it appear that, as you follow the movieclip, you are subtly exploring a scene
*/

//This is an example for a game that has 3 levels
//background changes depending on the level you are on
//so…
var level:Number = 1;

//PAN FOR TOP IMAGE
function moveImg(clip:Object, targ:Object, sizeW:Number, sizeH:Number) {
//sizeW/sizeH — the lower the numbers the larger the clip
function clip_onEnterFrame(event:Event) {
//distance, friction, target and size…
var this_clip = event.currentTarget;
this_clip.x += Math.ceil(((-Math.ceil(targ.x/sizeW))-this_clip.x)/20);
this_clip.y += Math.ceil(((-Math.ceil(targ.y/sizeH))-this_clip.y)/20);
};
//
function removed(event:Event){
event.currentTarget.removeEventListener(Event.ENTER_FRAME, clip_onEnterFrame);
event.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, removed);
}
clip.addEventListener(Event.REMOVED_FROM_STAGE,removed);
clip.addEventListener(Event.ENTER_FRAME, clip_onEnterFrame);
//
};

//PAN FOR LOWER IMAGE
function moveLowImg(clip:Object, targ:Object, sizeW:Number, sizeH:Number, fade:Number) {
var ystart:Number = clip.y;
clip.x = Math.ceil(stage.stageWidth/2);
//
function clip_onEnterFrame(event:Event) {
var this_clip = event.currentTarget;
var targY:Number = ystart-(targ.y/sizeH)+150
var targX:Number = targ.x – (targ.x * 2)+sizeW
//
this_clip.y += Math.ceil((targY-clip.y)/20);
this_clip.x += Math.ceil((targX-clip.x)/20);
//
this_clip.scaleY += (((targ.y/fade)/100)-clip.scaleY)/20;
this_clip.scaleX += (((targ.y/fade)/100)-clip.scaleX)/20;
};
//
function removed(event:Event){
event.currentTarget.removeEventListener(Event.ENTER_FRAME, clip_onEnterFrame);
event.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, removed);
}
clip.addEventListener(Event.REMOVED_FROM_STAGE,removed);
clip.addEventListener(Event.ENTER_FRAME, clip_onEnterFrame);
//
}

moveImg(mc_1, mc_movieClipToFollow, .8, 1);
moveLowImg(mc_2, movieClipToFollow, 700, 2, 5);

———–

MovieClip To Mouse (Simple Inertia)

(DOWNLOAD SOURCE )
[swfobj src=”http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/movieclip_to_mouse_inertia.swf” height=”300″ width=”300″]

Code:

//inertia movement on clip
function inertia(clip:MovieClip, speed:Number) {
function this_onEnterFrame(event:Event) {
//or event.target.stage.mouseY
clip.x += Math.ceil((mouseX-clip.x)/speed);
clip.y += Math.ceil((mouseY-clip.y)/speed);
};
clip.addEventListener(Event.ENTER_FRAME, this_onEnterFrame);
}

inertia(mc_bgFollow, 10);

———–

Cat In Space (Scrolling Stars)
(DOWNLOAD SOURCE )
[swfobj src=”http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/nested_addChild_catinspace.swf” height=”597″ width=”674″]

———–

Particle Effects For Mouse
(DOWNLOAD SOURCE )
[swfobj src=”http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/particle_effect_in_container_clip.swf” height=”300″ width=”350″]

———–

Pass Property As String (Organic Values)
(DOWNLOAD SOURCE )
[swfobj src=”http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/pass_property_as_string.swf” height=”100″ width=”100″]

Code:

//Send a property to function as a string reference
//Complex movement as various properties
//
function randomMovement(clip:MovieClip, mcProp:String, maxVal:Number, minVal:Number, decay:Number, decimal:Number) {
//init variables
var ymoveval:Number = 0;
var yPos:Number = 0;
var yTarget:Number = 0;
var yval:Number = 0;
var rVar:Number = 0;
var fVar:Number = 0;
var yVar:Number = 0;
//
function clip_onEnterFrame(event:Event) {
if (ymoveval<0.1 && ymoveval>-0.1) {
rVar = ((Math.ceil(Math.random()*4)+4)/100);
fVar = ((Math.ceil(Math.random()*4)+4)/10);
yVar = (Math.random()*(maxVal-minVal))+minVal;
}
//
yPos = (yPos*fVar)+((yVar-yTarget)*rVar);
yTarget = yTarget+yPos;
ymoveval = (yTarget-yval)*decimal+(ymoveval*decay);
yval = yval+ymoveval;
clip[mcProp] = yval;
};
clip.addEventListener(Event.ENTER_FRAME, clip_onEnterFrame);
}

randomMovement(mc_test, “rotation”, 360, -360, 0.1, 0.01);

———–

Random Colors
(DOWNLOAD SOURCE)
[swfobj src=”http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/random_colors.swf” height=”224″ width=”478″]

Code:

//Apporach #1
function changeColFunc(myMc:MovieClip) {
var randCol:ColorTransform = myMc.transform.colorTransform;
randCol.color = (Math.random()*256 < < 16 | Math.random()*256 << 8 | Math.random()*256); myMc.transform.colorTransform = randCol; }; //Apporach #2 function changeColFunc2(myMc:MovieClip) { var randCol:ColorTransform = myMc.transform.colorTransform; randCol.color = (Math.ceil(Math.random()*999999)); myMc.transform.colorTransform = randCol; }; ----------- It's always fun to see all the little bits and pieces...