Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Texture movement when score hits 200

$
0
0
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

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>