View Full Version : Please tell me how to make a highscore list?
bluemagica
April 14th, 2008, 12:56 PM
Well, i need to make a highscore list, but i need to make it using textfiles! but since i am very new at programming i cant figure out how to sort the the list...i want to sort the list within flash, but how do i make it so that the names and the scores get sorted together? currently i am thinking of somehow using sortOn on the array generated using "split"....but since the information resides on a external text file, therefore this method might be very slow in practice, and especially when my target viewers are dial-up users. So what do you guys suggest?
ArmoredSandwich
April 14th, 2008, 01:07 PM
Well, i need to make a highscore list, but i need to make it using textfiles! but since i am very new at programming i cant figure out how to sort the the list...i want to sort the list within flash, but how do i make it so that the names and the scores get sorted together? currently i am thinking of somehow using sortOn on the array generated using "split"....but since the information resides on a external text file, therefore this method might be very slow in practice, and especially when my target viewers are dial-up users. So what do you guys suggest?
What has dial up to do with the method of sorting the array?
What is the lay out of the textfile?
Jerryscript
April 14th, 2008, 06:01 PM
You don't need to return all highscores to flash, only the amount you want to list, usually around 5 to 10, as well as the user's.
Pass the name/score pair to your backend script, have it update the text file (if using PHP, the split, search and replace functions are similar to flash's), and return the above amount of data in a string of "name,score,name,score,name,score" etc. You can then do something like this example simulation to parse the returned data and sort it for display:
// create some textfields
createTextField('playerText', getNextHighestDepth(), 100, 100, 100, 20);
playerText.text = 'Enter Player Name';
createTextField('player', getNextHighestDepth(), 100, 140, 100, 20);
player.border = true;
player.type = 'input';
createTextField('scoreText', getNextHighestDepth(), 100, 200, 100, 20);
scoreText.text = 'Enter Sample Score';
createTextField('score', getNextHighestDepth(), 100, 240, 100, 20);
score.border = true;
score.type = 'input';
createEmptyMovieClip('activator', getNextHighestDepth());
activator._x = 150; activator._y = 300;
activator.createTextField('activatorButton', activator.getNextHighestDepth(), 0, 0, 0, 0);
with(activator.activatorButton){ border = true; selectable = false; autoSize = 'center'; text = 'Press to Simulate'; }
createTextField('highScoreList', getNextHighestDepth(), 300, 50, 300, 450);
with(highScoreList){ border = wordWrap = multiline = true; }
// this string is a simulation of the string you will be receiving after sending your name/score to the server
updatedList = 'Adam,'+Math.round(Math.random() * 10000)+',Barry,'+Math.round(Math.random() * 10000)+',Charles,'+Math.round(Math.random() * 10000)+',Darren,'+Math.round(Math.random() * 10000)+',Ernest,'+Math.round(Math.random() * 10000)+',Frank,'+Math.round(Math.random() * 10000)+',George,'+Math.round(Math.random() * 10000)+',Arianna,'+Math.round(Math.random() * 10000)+',Brittany,'+Math.round(Math.random() * 10000)+',Christine,'+Math.round(Math.random() * 10000)+',Dorothy,'+Math.round(Math.random() * 10000)+',Elizabeth,'+Math.round(Math.random() * 10000)+',Francine,'+Math.round(Math.random() * 10000)+',Gretchen,'+Math.round(Math.random() * 10000);
// this would be in an onLoad or onData type of function
activator.onPress = function(){
// split it up at the commas
splitList = updatedList.split(',');
// seed your score list with the name/score pairs as properties of an object in an array
scoreList = new Array();
for(var l = 0; l < splitList.length; l += 2){
scoreList.push( { name: splitList[l], score: Number(splitList[l + 1]) } );
}
// this next line is just for this simulation, it will be handled by the backend script in your app
scoreList.push( { name: player.text, score: Number(score.text) } );
// sort by numeric scores in descending order
scoreList.sortOn('score', Array.NUMERIC | Array.DESCENDING);
// display the highscore list
for(var i = 0; i < scoreList.length; i++){
highScoreList.text += scoreList[i].name + ' ' + scoreList[i].score + '
';
}
};
bluemagica
April 14th, 2008, 11:52 PM
Thanks Jerryscript, this is kinda like what i did....my text file is like
var_text=john,26 sam,24 dead,18 last,19
I, similar to you, split the string first at " " and then at "," giving the required array, and then used same technique as yours... but the problem is, though i can get the highscore, it is showing up a bit late...i dont want that time lag.
But anyway, i really learnt a lot from your code, thanks for the help again JerryScript.
EDIT:: now i have a problem.
p0.text="Game"; works
this["p"+i].text="Game"; dosent work, anybody know why?
Jerryscript
April 15th, 2008, 10:38 AM
I'm not sure what the problem is with your code without seeing more of it. Maybe a scope issue since you're using "this".
The lag issue is something you have to learn to live with, but hopefully it's not more than 150ms or so. Just toss in some sort of fancy particle effect or other animation that your highscore list emerges from, and by the time the effect is done, enough time should have elapsed for your list to be loaded, parsed, sorted, and rendered.
bluemagica
April 15th, 2008, 10:47 AM
well it is fixed now, and i have a new problem. The texts which are rendered do not have the formatting of the dynamic textbox. by formatting i mean the font and "bold"....when the text comes up it is not bold and in times new roman font....any ideas why??
rrh
April 15th, 2008, 01:57 PM
Did you embed the font?
bluemagica
April 15th, 2008, 11:42 PM
Nope, cause as soon as i embed the font, for some weird reason, the texts dissappear.
Jerryscript
April 16th, 2008, 12:16 PM
AS2 - check the textFormat and newTextFormat
AS3 - check the default textformat
bluemagica
April 17th, 2008, 01:11 AM
Hmm, jerryscript, does that mean i have to set the formats for all the fields in as?
And anyway i have a BIG PROBLEM on me. I am using "split_array.sortOn('plscore', Array.DESCENDING | Array.NUMERIC)". the array was 99,88,69,7,8, and it is coming 99,8,88,7,69
Why is this?
rrh
April 17th, 2008, 12:34 PM
It looks like it's sorting them as strings, not as numbers.
Jerryscript
April 17th, 2008, 02:35 PM
I'm pretty sure the problem is that you are not seeding your plscores as numbers. They do not become numbers automatically, you have to convert them using Number() or int() or uint().
In the code I posted above, it sorts your list of numbers fine. However, if you leave out the Number() statement when creating the array of objects, it gives the same results as you showed because the string is never converted to a number.
// note the "score: Number(score.text)" statement
scoreList.push( { name: player.text, score: Number(score.text) } );
bluemagica
April 17th, 2008, 10:39 PM
actually i managed to solve it using parseInt(). but thanks for your replies...
Anyway, this might sound a bit oftopic, but recently i saw a firefox extension, that enables to modify the outgoing flash data like score, resulting in a possible hack. For those who know about this, is there any solution against this?
Jerryscript
April 18th, 2008, 01:51 AM
Try some form of encryption and code obfuscation. Nothing is 100% safe, but that should stop most.
bluemagica
April 18th, 2008, 02:05 AM
hmm but since i dont know php well, i cant really decrypt it properly on the server side....ah well i guess i will have to learn php now.
Jerryscript
April 18th, 2008, 06:09 PM
There are classes available to use with PHP in almost the same way you use classes with ActionScript. It is a very easy migration, AS to PHP, and the online PHP manual has user contributed notes with code snippets that will help you do just about anything you need to.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.