Creation of a Flash highscores API - Step 2

Now that you know how to communicate between two Flash movies thanks to the Creation of a Flash highscores API tutorial, it's time to introduce the next step that will make you save your highest score on your computer.

It's not an hard prototype once you read Create an Eskiv Flash game tutorial (in order to have a real game) and Managing savegames with Flash shared objects (to store data on your computer)

This is the game:

ACTIONSCRIPT:
  1. var send_score:LocalConnection = new LocalConnection();
  2. _root.attachMovie("score", "score", 1);
  3. _root.attachMovie("hero", "hero", 2, {_x:250, _y:200});
  4. _root.attachMovie("target", "target", 3, {_x:Math.random()*400+25, _y:Math.random()*300+25});
  5. enemy_power = 3;
  6. points = 0;
  7. max_score = 0;
  8. hero.onEnterFrame = function() {
  9.     this._x = _root._xmouse;
  10.     this._y = _root._ymouse;
  11. };
  12. target.dir = Math.random()*2*Math.PI;
  13. target.xspeed = enemy_power*Math.cos(foe.dir);
  14. target.yspeed = enemy_power*Math.sin(foe.dir);
  15. target.onEnterFrame = function() {
  16.     dist_x = this._x-hero._x;
  17.     dist_y = this._y-hero._y;
  18.     distance = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
  19.     if (distance<(hero._width+this._width)/2) {
  20.         points++;
  21.         if (points>max_score) {
  22.             max_score = points;
  23.             send_score.send("hall_of_fame", "compare_scores", points);
  24.         }
  25.         score.scoretxt.text = points;
  26.         this._x = Math.random()*400+25;
  27.         this._y = Math.random()*300+25;
  28.         foe = _root.attachMovie("enemy", "enemy", _root.getNextHighestDepth(), {_x:Math.random()*400+25, _y:Math.random()*300+25});
  29.         foe.dir = Math.random()*2*Math.PI;
  30.         foe.xspeed = enemy_power*Math.cos(foe.dir);
  31.         foe.yspeed = enemy_power*Math.sin(foe.dir);
  32.         foe.onEnterFrame = function() {
  33.             this._x += this.xspeed;
  34.             this._y += this.yspeed;
  35.             if ((this._x<0) or (this._x>500)) {
  36.                 this.xspeed *= -1;
  37.             }
  38.             if ((this._y<0) or (this._y>400)) {
  39.                 this.yspeed *= -1;
  40.             }
  41.             dist_x = this._x-hero._x;
  42.             dist_y = this._y-hero._y;
  43.             distance = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
  44.             if (distance<(hero._width+this._width)/2) {
  45.                 points--;
  46.                 score.scoretxt.text = points;
  47.                 this.removeMovieClip();
  48.             }
  49.         };
  50.     }
  51. };

The game game developer only needs to add line 1 and line 23 to his game

and this is the highscore table

ACTIONSCRIPT:
  1. var get_score:LocalConnection = new LocalConnection();
  2. get_score.connect("hall_of_fame");
  3. top_score = SharedObject.getLocal("feronato");
  4. if (top_score.data.score == undefined) {
  5.     top_score.data.score = 0;
  6. }
  7. _root.topscore.text = "Highest score: "+top_score.data.score;
  8. get_score.compare_scores = function(points):Void  {
  9.     if (points>top_score.data.score) {
  10.         top_score.data.score = points;
  11.     }
  12.     _root.currentscore.text = "Score: "+points;
  13.     _root.topscore.text = "Highest score: "+top_score.data.score;
  14. };

Now play the game here, move the purple circle with the mouse, pick up the red circle, avoid the blue ones.

and see your best performance here.

Even if you reload, or close the page and open it again.

I noticed sometimes I need to reload the page in order to make it works. Does it happen to you too?

Having a complete online leaderboard is a long journey, but we'll have it soon

Meanwhile, download the source codes and experiment

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

11 Responses to “Creation of a Flash highscores API - Step 2”

  1. JDog on May 15th, 2008 11:29 pm

    Thats great ! Is there a way to have a full ingame leaderboard which saves ? Thats a feature that i’d be tempted to add, should I gain the knowledge of how to do it !

  2. Andy Cook on May 15th, 2008 11:31 pm

    Worked when I reloaded. 52 collected.

    Thanks a lot for the tutorial

  3. Javier Lázaro on May 16th, 2008 2:18 am

    Awesome!
    Thanks a lot!

  4. Grifo on May 16th, 2008 3:26 am

    Good work.

    I didn’t have load problems.

    Also, gameplay is much better than the original one.

    Luck.

  5. Xodus on May 16th, 2008 5:05 pm

    It works fine for me. I like this one better than the other eskiv game, much better.

    I got 41 as my highscore!! XD

  6. styxtwo on May 16th, 2008 5:52 pm

    it works, but the score and highscore give the same number always.

  7. gartman222 on May 17th, 2008 10:40 pm

    got 51

  8. Graham on May 18th, 2008 11:20 pm

    59!!!!

  9. Sushi on July 18th, 2008 3:14 pm

    60 :D!! Its a fun game haha

  10. giridhar on September 1st, 2008 10:33 am

    its very helpful for me,
    display the making very low end games.
    and some technical scripts to go throuth some script books,plz.thanks

  11. shawn on September 19th, 2008 6:09 am

    It’s having trouble running in flash mx. Unexpected file format, and when i type it in manually, the hignscore list shows this.
    **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: There is no property with the name ‘compare_scores’.
    get_score.compare_scores = function(points) {
    Is this only ment to work on newer flash programs?

Leave a Reply