My current score output is 'SCORE: 0' written twice (one in red letters the other in yellow) in the upper left corner of the screen but with slightly different positions to give the letters/numbers a retro overlapping look. Only one score, but two layers of the same score.
The problem is that only the red layer of the score updates while the yellow layer stays at 0.
I am creating two objects of my ScoreFont class in a FontManager class (see below) with different colors/positions to give that overlapping feel (yellow on red) with the following code;
fontList.Add(new ScoreFont(Game.Content.Load<SpriteFont>(@"Fonts\Score"), scoreText, new Vector2 (5, -5), Color.Yellow, 1f, 0f));
//Adding this to make the score font stick out more by layering it
fontList.Add(new ScoreFont(Game.Content.Load<SpriteFont>(@"Fonts\Score"), scoreText, new Vector2(9, -5), Color.Red, 1f, 0.1f));
You'll notice that the first ScoreFont object created is the yellow one (the one I"m trying to fix), the second being red (the one displaying the correct score). If I reversed these then the yellow would work but the red would not.
I am updating/drawing the list in the FontManager class with a for loop;
for (int i = 0; i < fontList.Count; i++)
{
Font f = fontList[i];
f.Draw/Update(gameTime, sB);
}
The ScoreFont.cs class which is derived from the Font.cs class contains;
class ScoreFont : Font
{
public string scoreText;
public int totalScore = 0;
public static ScoreFont sF;
//Constructor
public ScoreFont(parameters)
:base (parameters)
{sF = this;}
//Add score
public void AddScore(int score)
{
totalScore += score;
}
//Draw
public override void Draw(GameTime g, SpriteBatch s)
{
scoreText = "Score: " + totalScore;
s.DrawString(scoreText,blah, blah, ...);
}
And I am updating the score by calling the AddScore method in a different class when the sprites go off the screen via;
ScoreFont.sF.AddScore(spriteList[i].score);
This works great if I'm only creating one layer of the score but when I try to create two, the top most layer (the yellow one) doesn't update.
How do I fix this?
Beside the very awkward use of the Font class, your problem is in:
ScoreFont.sF.AddScore(spriteList[i].score);
Why aren't you looping in all your fontList item to set each of them with the new score value?
foreach(ScoreFont font in fontList)
font.AddScore(myScore);
You implement a singleton pattern with the static field name sF
but somehow you have multiple instances to update.
Or, if your plan was to have only a single score, then the AddScore
and the totalScore
field should also have been static.