0
votes

I'm doing a simple Tower Defense game on XNA and I'm having some trouble. I have an enemy sprite that should be green, the image is correct, but when I insert it on the program it appears all black, but in the right shape. Here's my code (sorry that it's a mess):

namespace MicroDefense
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    enum GameState
    {
        MainMenu,
        Options,
        Playing,
    }
    GameState CurrentGameState = GameState.MainMenu;

    // Screen Adjustments
    int screenWidth = 800, screenHeight = 600;

    cButton btnPlay;

    Level level = new Level();

    WaveManager waveManager;

    Player player;

    GUI.Button arrowButton;
    GUI.Button spikeButton;
    GUI.Button slowButton;

    GUI.Toolbar toolBar;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        // The width of the level in pixels
        graphics.PreferredBackBufferWidth = level.Width * 32;
        // The height of the toolbar + the height of the level in pixels
        graphics.PreferredBackBufferHeight = 32 + level.Height * 32;

        graphics.ApplyChanges();

        IsMouseVisible = true;
    }

    protected override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {

        spriteBatch = new SpriteBatch(GraphicsDevice);

        // Screen stuff
        graphics.PreferredBackBufferWidth = screenWidth;
        graphics.PreferredBackBufferHeight = screenHeight;
        graphics.ApplyChanges();
        IsMouseVisible = true;

        btnPlay = new cButton(Content.Load<Texture2D>("Button"), graphics.GraphicsDevice);
        btnPlay.setPosition(new Vector2(350, 300));


        Texture2D topBar = Content.Load<Texture2D>("tool bar");
        SpriteFont font = Content.Load<SpriteFont>("Arial");

        toolBar = new GUI.Toolbar(topBar, font, new Vector2(0, level.Height * 32));

        Texture2D grass = Content.Load<Texture2D>("grass");
        Texture2D path = Content.Load<Texture2D>("path");

        level.AddTexture(grass);
        level.AddTexture(path);

        Texture2D bulletTexture = Content.Load<Texture2D>("bullet");

        Texture2D[] towerTextures = new Texture2D[]
        {
            Content.Load<Texture2D>("arrow tower"),
            Content.Load<Texture2D>("spike tower"),
            Content.Load<Texture2D>("slow tower"),
        };

        player = new Player(level, towerTextures, bulletTexture);

        Texture2D enemyTexture = Content.Load<Texture2D>("enemy");
        Texture2D healthTexture = Content.Load<Texture2D>("health bar");

        waveManager = new WaveManager(player, level, 24, enemyTexture,
                                                         healthTexture);

        // The "Normal" texture for the arrow button.
        Texture2D arrowNormal = Content.Load<Texture2D>("GUI\\Arrow Tower\\arrow button");
        // The "MouseOver" texture for the arrow button.
        Texture2D arrowHover = Content.Load<Texture2D>("GUI\\Arrow Tower\\arrow hover");
        // The "Pressed" texture for the arrow button.
        Texture2D arrowPressed = Content.Load<Texture2D>("GUI\\Arrow Tower\\arrow pressed");

        // The "Normal" texture for the spike button.
        Texture2D spikeNormal = Content.Load<Texture2D>("GUI\\Spike Tower\\spike button");
        // The "MouseOver" texture for the spike button.
        Texture2D spikeHover = Content.Load<Texture2D>("GUI\\Spike Tower\\spike hover");
        // The "Pressed" texture for the spike button.
        Texture2D spikePressed = Content.Load<Texture2D>("GUI\\Spike Tower\\spike pressed");

        // The "Normal" texture for the slow button.
        Texture2D slowNormal = Content.Load<Texture2D>("GUI\\Slow Tower\\slow button");
        // The "MouseOver" texture for the slow button.
        Texture2D slowHover = Content.Load<Texture2D>("GUI\\Slow Tower\\slow hover");
        // The "Pressed" texture for the slow button.
        Texture2D slowPressed = Content.Load<Texture2D>("GUI\\Slow Tower\\slow pressed");

        // Initialize the arrow button.
        arrowButton = new GUI.Button(arrowNormal, arrowHover,
            arrowPressed, new Vector2(0, level.Height * 32));

        // Initialize the spike button.
        spikeButton = new GUI.Button(spikeNormal, spikeHover,
            spikePressed, new Vector2(32, level.Height * 32));

        // Initialize the slow button.
        slowButton = new GUI.Button(slowNormal, slowHover,
            slowPressed, new Vector2(32 * 2, level.Height * 32));

        //arrowButton.Clicked += new EventHandler(arrowButton_Clicked);
        //spikeButton.Clicked += new EventHandler(spikeButton_Clicked);
        //slowButton.Clicked += new EventHandler(slowButton_Clicked);

        arrowButton.OnPress += new EventHandler(arrowButton_OnPress);
        spikeButton.OnPress += new EventHandler(spikeButton_OnPress);
        slowButton.OnPress += new EventHandler(slowButton_OnPress);
    }

    protected override void UnloadContent()
    {

    }

    private void arrowButton_Clicked(object sender, EventArgs e)
    {
        player.NewTowerType = "Arrow Tower";
        player.NewTowerIndex = 0;
    }
    private void spikeButton_Clicked(object sender, EventArgs e)
    {
        player.NewTowerType = "Spike Tower";
        player.NewTowerIndex = 1;
    }
    private void slowButton_Clicked(object sender, EventArgs e)
    {
        player.NewTowerType = "Slow Tower";
        player.NewTowerIndex = 2;
    }

    private void arrowButton_OnPress(object sender, EventArgs e)
    {
        player.NewTowerType = "Arrow Tower";
        player.NewTowerIndex = 0;
    }
    private void spikeButton_OnPress(object sender, EventArgs e)
    {
        player.NewTowerType = "Spike Tower";
        player.NewTowerIndex = 1;
    }
    private void slowButton_OnPress(object sender, EventArgs e)
    {
        player.NewTowerType = "Slow Tower";
        player.NewTowerIndex = 2;
    }

    public Vector2 size;

    protected override void Update(GameTime gameTime)
    {
        MouseState mouse = Mouse.GetState();
        switch (CurrentGameState)
        {
            case GameState.MainMenu:
                if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
                btnPlay.Update(mouse);
                if (cButton.control)
                {
                    graphics.PreferredBackBufferHeight = 288;
                    graphics.PreferredBackBufferWidth = 256;
                    graphics.ApplyChanges();
                }
                break;

            case GameState.Playing:
                player.Update(gameTime, waveManager.Enemies);
            waveManager.Update(gameTime);


            //Update the arrow button.
            arrowButton.Update(gameTime);
            //Update the spike button.
            spikeButton.Update(gameTime);
            //Update the slow button.
            slowButton.Update(gameTime);
                break;
        }

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();

        switch (CurrentGameState)
        {
            case GameState.MainMenu:
                spriteBatch.Draw(Content.Load<Texture2D>("MainMenu"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
                btnPlay.Draw(spriteBatch);
                break;

            case GameState.Playing:
                level.Draw(spriteBatch);
                player.Draw(spriteBatch);
                waveManager.Draw(spriteBatch);

                // Draw the tool bar first,
                toolBar.Draw(spriteBatch, player);
                // and then our buttons.
                arrowButton.Draw(spriteBatch);
                spikeButton.Draw(spriteBatch);
                slowButton.Draw(spriteBatch);

                player.DrawPreview(spriteBatch);
                break;
        }
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

}

Edit1:The sprite has the right shape but the color is black.

Edit2: Here's the wave manager:

public class WaveManager
{
    private int numberOfWaves; // How many waves the game will have
    private float timeSinceLastWave; // How long since the last wave ended

    private Queue<Wave> waves = new Queue<Wave>(); // A queue of all our waves

    private Texture2D enemyTexture; // The texture used to draw the enemies

    private bool waveFinished = false; // Is the current wave over?

    private Level level; // A reference to our level class.

    public Wave CurrentWave // Get the wave at the front of the queue
    {
        get { return waves.Peek(); }
    }
    public List<Enemy> Enemies // Get a list of the current enemeies
    {
        get { return CurrentWave.Enemies; }
    }
    public int Round // Returns the wave number
    {
        get { return CurrentWave.RoundNumber + 1; }
    }

    public WaveManager(Player player, Level level, int numberOfWaves,
        Texture2D enemyTexture, Texture2D healthTexture)
    {
        this.numberOfWaves = numberOfWaves;
        this.enemyTexture = enemyTexture;

        this.level = level;

        for (int i = 0; i < numberOfWaves; i++)
        {
            int initialNumerOfEnemies = 6;//Número de inimigos por wave <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            int numberModifier = (i / 6) + 1;

            // Pass the reference to the player, to the wave class.
            Wave wave = new Wave(i, initialNumerOfEnemies * numberModifier,
                player, level, enemyTexture, healthTexture);

            waves.Enqueue(wave);
        }

        StartNextWave();
    }

    private void StartNextWave()
    {
        if (waves.Count > 0) // If there are still waves left
        {
            waves.Peek().Start(); // Start the next one

            timeSinceLastWave = 0; // Reset timer
            waveFinished = false;
        }
    }

    public void Update(GameTime gameTime)
    {
        CurrentWave.Update(gameTime); // Update the wave

        if (CurrentWave.RoundOver) // Check if it has finished
        {
            waveFinished = true;
        }

        if (waveFinished) // If it has finished
        {
            timeSinceLastWave += (float)gameTime.ElapsedGameTime.TotalSeconds; // Start the timer
        }

        if (timeSinceLastWave > 10.0f) // If 10 seconds has passed Tempo entre waves <<<<<<<<<<<<<
        {
            waves.Dequeue(); // Remove the finished wave
            StartNextWave(); // Start the next wave
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        CurrentWave.Draw(spriteBatch);
    }
}
1
sprite is shown as black rectangle or all pixels are black (except transparent)?Davor Mlinaric
Remove unrelevant code(such as event handlers, commented out lines of code, empty Initialize method(it gives no useful info) and so on). Also, could you provide your WaveManager source code? Looks like this class handles enemy drawing and root of the problem might be there. But still, try to avoid posting useless info.CodingFeles
all pixels are black, but the shape is correct. Sorry for the useless info, i'm new on the site and kind new on programmingFelipe Sadoyama
Seems trouble in Wawe.Draw() method. Perhapse you by mistake pass to spritebatch.Draw() black color instead of white?user4632995

1 Answers

0
votes

Without seeing your Wave class I can't be certain but my guess is your Draw function for it looks something like this:

spriteBatch.Draw(
  Content.Load<Texture2D>("Wave"), 
  new Rectangle(0, 0, width, height), 
  Color.Black);

When it should look like this (note the change in color):

spriteBatch.Draw(
  Content.Load<Texture2D>("Wave"), 
  new Rectangle(0, 0, width, height), 
  Color.White);