7 Replies - 2919 Views - Last Post: 14 February 2013 - 01:26 PM

#1 AllegroGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-October 12

Texture movement when score hits 200

Posted 14 February 2013 - 12:09 PM

Ok so this may seem like a basic programming question, but i'll go for it anyhow.
I'm programming a 2D space shooter in XNA and i want a texture i created to slide in from the side of the screen then slide back out on a timer when my score hits 200. The problem I am having is that my score hits 200 and if i dont kill anything after that the texture displaying "200 Points!!" slides in and out like i planned. But if i hit anything else after 200 points the texture stops (because im not == 200 anymore).

I have the movement/timing of the texture in a separate function. My question is how do i get the function to run to the end even if my points dont stay at 200 for the entire time?

What i have right now.. Pasted my whole HUD class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XNAshooterTutorial
{
    public class HUD
    {
        public int playerScore;
        public Texture2D score500;
        public Vector2 scoreUpdatePosition;
        public SpriteFont georgiaFont;
        public int screenHeight, screenWidth, scoreDisplayTimer;    
        public bool showHud;
        public string scoreText;
        public bool scoreAlert;

        // Constructor
        public HUD()
        {
            scoreDisplayTimer = 200;
            scoreAlert = false;
            scoreText = null;
            score500 = null;
            playerScore = 0;
            showHud = true;
            screenHeight = 950;
            screenWidth = 800;
            georgiaFont = null;
            scoreUpdatePosition = new Vector2(700, 800);
        }

        // Load Content
        public void LoadContent(ContentManager Content)
        {
            score500 = Content.Load<Texture2D>("500points");
            georgiaFont = Content.Load<SpriteFont>("fonts/georgia");
        }

        // Update
        public void Update(GameTime gameTime)
        {
            // Getting Keyboard State
            KeyboardState keyState = Keyboard.GetState();
            scoreText = "Score - " + playerScore;

            // Display score texture when player hits 200 points.
            if (playerScore == 200)
                UpdateScoreFlash();
        }

        // Draw
        public void Draw(SpriteBatch spriteBatch)
        {
            // If we are showing our HUD (or showHud is == true) then display our heads up display
            if (showHud == true)
            {                
                Vector2 scoreLength;
                scoreLength = georgiaFont.MeasureString(scoreText);
                spriteBatch.Draw(score500, scoreUpdatePosition, Color.White);
                spriteBatch.DrawString(georgiaFont, scoreText, new Vector2(screenWidth / 2 - scoreLength.X / 2, 25), Color.Red);
            }            
        }

        // Update Score Flash
        public void UpdateScoreFlash()
        {
            
                scoreDisplayTimer--;

                if (scoreDisplayTimer <= 200 && scoreDisplayTimer > 100)
                    scoreUpdatePosition.X -= 3;

                else if (scoreDisplayTimer < 100 && scoreDisplayTimer > 0)
                    scoreUpdatePosition.X += 3;           

            // Reset Score Timer
            if (scoreDisplayTimer == 0)
            {
                scoreDisplayTimer = 200;
            }
        }
    }
}



Main issues are at lines 52-54 in my Update and from line 70 which is my score texture movment/timing function

Is This A Good Question/Topic? 0
  • +

Replies To: Texture movement when score hits 200

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Texture movement when score hits 200

Posted 14 February 2013 - 12:16 PM

I'd suggest changing it around a bit so that in the update method you call the UpdateScoreFlash method without it being inside an if check. You can then add an if check in the UpdateScoreFlash method that will initialize the movement, and the Update method will handle the rest every frame afterwards.
Was This Post Helpful? 0
  • +
  • -

#3 AllegroGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-October 12

Re: Texture movement when score hits 200

Posted 14 February 2013 - 12:23 PM

Ok so i changed my updatescoreflash function to this, and i just call the function in the update. It still seems to stop running the function if i go over 200. Thanks for the quick response :) any other ideas?

// Update Score Flash
        public void UpdateScoreFlash()
        {
            if (playerScore == 200)
            {
                scoreDisplayTimer--;

                if (scoreDisplayTimer <= 200 && scoreDisplayTimer > 100)
                    scoreUpdatePosition.X -= 3;

                else if (scoreDisplayTimer < 100 && scoreDisplayTimer > 0)
                    scoreUpdatePosition.X += 3;
                
            }

            // Reset Score Timer
            if (scoreDisplayTimer == 0)
            {
                scoreDisplayTimer = 200;
            }
        }


Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Texture movement when score hits 200

Posted 14 February 2013 - 12:54 PM

Before we go further, let me get a better idea on what exactly you're trying to achieve. Are you trying to have the score scroll across the screen at certain intervals as a nice little effect to make the player feel like they're accomplishing something?

If so, then I'd redo the entire way this is done. I'd create a bool that will keep track of whether the score scroll is supposed to be on the screen or not. Once the score hits one of the predetermined values, such as 200, you flip the bool to true. While this bool is set to true, the Update Method will call the UpdateScoreFlash method which will move the score across the screen. Once the score has reached the end of it's scroll, which would be after it exits the screen, you flip this bool back to false.

Here's a bit of pseudo-code to give you a better idea of what I'm thinking you're trying to accomplish:
int score = 0;
bool scoreShow = false;

public void Update(GameTime gameTime)
{
    //... Your code up here that pertains to other things

    if (score == 200)
    {
        scoreShow = true;
    }

    if (scoreShow)
    {
        UpdateScoreFlash();
    }
}

// Update Score Flash
public void UpdateScoreFlash()
{
            
    scoreDisplayTimer--;
    if (scoreDisplayTimer <= 200 && scoreDisplayTimer > 100)
        scoreUpdatePosition.X -= 3;
    else if (scoreDisplayTimer < 100 && scoreDisplayTimer > 0)
        scoreUpdatePosition.X += 3;           

    // Reset Score Timer
    if (scoreDisplayTimer == 0)
    {
        showScore = false;
        scoreDisplayTimer = 200;
    }
}



With the code I've given here, this will only happen once the score hits 200. The flag will flip, and then no matter what your score is after that, the animation will continue until it reaches the end of the timer that you've created.

If you want this to display at other score amounts, then obviously the code will need to be altered to allow for this to happen, which would be quite easy, especially if the intervals are even each time such has 200, 400, 600, 800, etc. Each of these is 200+ more than the previous and can easily be handled using the %(modulo) operator with a simple if (score % 200 == 0) modification to the if check to flip the bool.
Was This Post Helpful? 0
  • +
  • -

#5 AllegroGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-October 12

Re: Texture movement when score hits 200

Posted 14 February 2013 - 01:09 PM

Ok This seems like a completely viable option, I've tried what you suggested and i don't see any movment at all from the image (Yea like you mentioned its just a cool little effect i wanted to add as a "way to go" type deal). Is it an issue with my timer amount? how long is 200 exactly in seconds?
Was This Post Helpful? 0
  • +
  • -

#6 AllegroGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-October 12

Re: Texture movement when score hits 200

Posted 14 February 2013 - 01:15 PM

Nevermind, That did the trick, had a line out of place!
Thanks tuns for the quick responses! Works like a charm :)
Was This Post Helpful? 0
  • +
  • -

#7 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Texture movement when score hits 200

Posted 14 February 2013 - 01:22 PM

Glad it worked out for you. Let us know if you run into any other issues with this, or if you plan to expand it into something more elaborate.
Was This Post Helpful? 0
  • +
  • -

#8 AllegroGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-October 12

Re: Texture movement when score hits 200

Posted 14 February 2013 - 01:26 PM

Posted Image

Thanks again!

View PostKilorn, on 14 February 2013 - 01:22 PM, said:

Glad it worked out for you. Let us know if you run into any other issues with this, or if you plan to expand it into something more elaborate.


Its actually part of a XNA 2D space shooter tutorial i've been making on youtube (Gamerdad81 here). And just wanted some cool stuff to work on for my subs that are following the series :)

* Tutorial Playlist Link
http://www.youtube.c...eature=view_all
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1