敌人精灵去黑XNA

时间:2015-09-21 12:40:20

标签: c# xna sprite

我正在XNA上做一个简单的塔防游戏,我遇到了一些麻烦。我有一个绿色的敌人精灵,图像是正确的,但当我将它插入程序时,它看起来全黑,但形状正确。 这是我的代码(对不起,这是一团糟):

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:精灵的形状正确但颜色为黑色。

Edit2:这是波浪经理:

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 个答案:

答案 0 :(得分:0)

在没有看到Wave课程的情况下,我无法确定,但我的猜测是您的Draw课程,因为它看起来像这样:

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

当它看起来像这样(注意颜色的变化):

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