Hot File

A Simple Score Display for Flash Games

View: 306    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Flash template, Flash game   Fields: Other

9 point/2 review File has been tested

Almost all games out there use a scoring system to help players see their progress.

Introduction

Almost all games out there use a scoring system to help players see their progress.

It is essential to show the player’s score in a clear and fun way. In this Quick Tip we're going to learn how to do just that!

Click the button to add 20,000 points to your score:

In this Quick Tip we’re going to learn how to create a score display. To improve the quality of our display, we're going to do two things:

  1. Add commas to our score, so it reads 1,600,000 instead of 1600000. This makes it easier for the player to figure out how big his or her score is.
  2. Make our score transition between values, instead of changing immediately. This gives the player a sense of achievement, because he or she actually sees his score grow.

In the end we’ll have a very simple and useful class, which you can easily use within any of your projects.

This class will only concern itself with displaying the score, not with calculating it.

First off let’s create our class; I’ve named it ScoreDisplay:

package
{
    import flash.display.Sprite;
     
    public class ScoreDisplay extends Sprite
    {
        public function ScoreDisplay()
        {
             
        }
    }
}

Let’s slowly add some variables:

package
{
    import flash.display.Sprite;
     
    public class ScoreDisplay extends Sprite
    {
        //the score which is being shown, whilst it is increasing
        public var currentScore:uint;
         
        //the player's score
        private var score:uint;
         
         
        public function ScoreDisplay()
        {
             
        }
    }
}

We’re going to show our score in a TextField. If you’d like to use a Symbol when working with ScoreDisplay, you won’t need to create the text field by code. However, if you don’t want to use a Symbol, you’ll need to call createScoreField().

Do remember that if you want to use your own Symbol, you must give the text field inside that symbol the instance name of currentScoreField.

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
     
    public class ScoreDisplay extends Sprite
    {
        //the text field which will show currentScore
        public var currentScoreField:TextField;
         
        //the score which is being shown, whilst it is increasing
        public var currentScore:uint;
         
        //the player's score
        private var score:uint;
         
        public function ScoreDisplay()
        {
             
        }
         
        //if the developer won't link this class to a symbol, this method must be called
        public function createScoreField():void
        {
            currentScoreField = new TextField();
            addChild(currentScoreField);
        }
    }
}

Now let’s start thinking what we’d like to do with our ScoreDisplay class. We’d like to be able to set a score, as well as add or subtract from the player’s score. So let’s create those methods!

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
     
    public class ScoreDisplay extends Sprite
    {
        //the text field which will show currentScore
        public var currentScoreField:TextField;
         
        //the player's score
        private var score:uint;
         
        //the score which is being shown, whilst it is increasing
        private var currentScore:uint;
         
        public function ScoreDisplay()
        {
             
        }
         
        //if the developer won't link this class to a symbol, this method must be called
        public function createScoreField():void
        {
            currentScoreField = new TextField();
            addChild(currentScoreField);
        }
         
        public function setScore(_value:uint):void
        {
            score = _value; 
        }
         
        public function changeScore(_change:uint):void
        {
            score += _change;
        }
    }
}

So far so good, we can now set and change the score’s value. But how will we display this? Even though it might not yet seem very useful, we’ll be using an enter frame event listener. Don’t worry it will make sense!

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
     
    public class ScoreDisplay extends Sprite
    {
        //the text field which will show currentScore
        public var currentScoreField:TextField;
         
        //the player's score
        private var score:uint;
         
        //the score which is being shown, whilst it is increasing
        private var currentScore:uint;
         
        public function ScoreDisplay()
        {
            addEventListener(Event.ENTER_FRAME, showScore, false, 0, true);
        }
         
        //if the developer won't link this class to a symbol, this method must be called
        public function createScoreField():void
        {
            currentScoreField = new TextField();
            addChild(currentScoreField);
        }
         
        public function setScore(_value:uint):void
        {
            score = _value; 
        }
         
        public function changeScore(_change:uint):void
        {
            score += _change;
        }
         
        private function showScore(event:Event):void
        {
            currentScoreField.text = String(score);
        }
    }
}

If we’d like to use our class in a project, it would look like this. Seems to work right - the score changes - but we aren't done. Remember what we wanted to do?

  1. Add commas to our score, so it reads 1,600,000 instead of 1600000.
  2. Make our score transition between values, instead of changing immediately.

Let’s start with the first goal, adding commas.

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
     
    public class ScoreDisplay extends Sprite
    {
        //the text field which will show currentScore
        public var currentScoreField:TextField;
         
        //the player's score
        private var score:uint;
         
        //the score which is being shown, whilst it is increasing
        private var currentScore:uint;
         
        public function ScoreDisplay()
        {
            addEventListener(Event.ENTER_FRAME, showScore, false, 0, true);
        }
         
        //if the developer won't link this class to a symbol, this method must be called
        public function createScoreField():void
        {
            currentScoreField = new TextField();
            addChild(currentScoreField);
        }
         
        public function setScore(_value:uint):void
        {
            score = _value; 
        }
         
        public function changeScore(_change:uint):void
        {
            score += _change;
        }
         
        private function showScore(event:Event):void
        {
            currentScoreField.text = addCommas(score);
        }
         
        private function addCommas(_score:uint):String
        {
            //a string, which will have the score with commas
            var scoreString:String = new String();
             
            //the amount of characters our score (without commas) has
            var scoreLength:uint = _score.toString().length;
            scoreString = "";
             
            //add the commas to the string
            for (var i:uint=0; i<scoreLength; i++) { 
                if ((scoreLength-i)%3 == 0 && i != 0) {
                    scoreString += ",";
                }
                scoreString += _score.toString().charAt(i);
            }
             
            return scoreString;
        }
    }
}

Now let’s work on our second goal; transitioning between score values, instead of changing to the new value immediately.

For this we can use the awesome capabilities of the Tween class. Most times we think of the Tween class for moving display objects, but you can use it to change any numerical value, including our score.

package
{
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
     
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
     
    public class ScoreDisplay extends Sprite
    {
        //the amount of time (in ms) which is needed to transition from one score value to another one
        private static const TRANSITION_LENGTH:uint = 500;
         
        //the score which is being shown, whilst it is increasing
        public var currentScore:uint;
         
        //the player's score
        private var score:uint;
         
        //the text field which will show currentScore
        private var currentScoreField:TextField;
         
        //this will tween the current score's value
        private var currentScoreTween:Tween;
         
        public function ScoreDisplay()
        {
            addEventListener(Event.ENTER_FRAME, showScore, false, 0, true);
        }
         
        //if the developer won't link this class to a symbol, this method must be called
        public function createScoreField():void
        {
            currentScoreField = new TextField();
            addChild(currentScoreField);
        }
         
        public function setScore(_value:uint):void
        {
            score = _value; 
             
            tweenCurrentScore();
        }
         
        public function changeScore(_change:uint):void
        {
            score += _change;
             
            tweenCurrentScore();
        }
         
        private function showScore(event:Event):void
        {
            currentScoreField.text = addCommas(currentScore);
        }
         
        private function tweenCurrentScore():void
        {
            currentScoreTween = new Tween(this, "currentScore", None.easeNone, currentScore, TRANSITION_LENGTH, true);
        }
         
        private function addCommas(_score:uint):String
        {
            //a string, which will have the score with commas
            var scoreString:String = new String();
             
            //the amount of characters our score (without commas) has
            var scoreLength:uint = _score.toString().length;
            scoreString = "";
             
            //add the commas to the string
            for (var i:uint=0; i<scoreLength; i++) { 
                if ((scoreLength-i)%3 == 0 && i != 0) {
                    scoreString += ",";
                }
                scoreString += _score.toString().charAt(i);
            }
             
            return scoreString;
        }
    }
}

 

A Simple Score Display for Flash Games

A Simple Score Display for Flash Games Posted on 14-01-2016  Almost all games out there use a scoring system to help players see their progress. 4.5/10 306

Comment:

To comment you must be logged in members.

Files with category

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd