C# 敌人的精灵变黑了XNA
我正在XNA上做一个简单的塔防游戏,我遇到了一些麻烦。我有一个应该是绿色的敌人精灵,图像是正确的,但当我将其插入程序时,它看起来全是黑色,但形状正确。 这是我的代码(很抱歉弄得一团糟):C# 敌人的精灵变黑了XNA,c#,xna,sprite,C#,Xna,Sprite,我正在XNA上做一个简单的塔防游戏,我遇到了一些麻烦。我有一个应该是绿色的敌人精灵,图像是正确的,但当我将其插入程序时,它看起来全是黑色,但形状正确。 这是我的代码(很抱歉弄得一团糟): 命名空间微防御 { 公共类游戏1:Microsoft.Xna.Framework.Game { 图形管理器图形; SpriteBatch SpriteBatch; 枚举配子状态 { 主菜单, 选项, 玩, } GameState CurrentGameState=GameState.main菜单; //屏幕调整
命名空间微防御
{
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
枚举配子状态
{
主菜单,
选项,
玩,
}
GameState CurrentGameState=GameState.main菜单;
//屏幕调整
int屏幕宽度=800,屏幕高度=600;
cButton btnPlay;
级别=新级别();
WaveManager WaveManager;
玩家;
按钮箭头按钮;
按钮;
按钮慢按钮;
工具栏;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
//级别的宽度(以像素为单位)
graphics.PreferredBackBufferWidth=level.Width*32;
//工具栏的高度+级别的高度(以像素为单位)
graphics.PreferredBackBufferHeight=32+级别。高度*32;
graphics.ApplyChanges();
IsMouseVisible=true;
}
受保护的覆盖无效初始化()
{
base.Initialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
//屏幕材料
graphics.PreferredBackBufferWidth=屏幕宽度;
graphics.PreferredBackBufferHeight=屏幕高度;
graphics.ApplyChanges();
IsMouseVisible=true;
btnPlay=新的cButton(Content.Load(“按钮”)、graphics.GraphicsDevice);
BTN显示设置位置(新矢量2(350300));
Texture2D topBar=内容加载(“工具栏”);
SpriteFont字体=Content.Load(“Arial”);
工具栏=新的GUI.toolBar(顶栏,字体,新矢量2(0,level.Height*32));
Texture2D grass=Content.Load(“grass”);
Texture2D路径=Content.Load(“路径”);
水平。添加纹理(草);
level.AddTexture(路径);
Texture2D bulletTexture=Content.Load(“bullet”);
纹理2D[]towerTextures=新纹理2D[]
{
内容物荷载(“箭头塔”),
内容物荷载(“尖塔”),
内容物荷载(“慢塔”),
};
玩家=新玩家(关卡、TowerTexture、bulletTexture);
Texture2D enemyTexture=Content.Load(“敌人”);
Texture2D healthTexture=Content.Load(“健康栏”);
waveManager=新的waveManager(玩家,级别,24,enemyTexture,
健康纹理);
//箭头按钮的“正常”纹理。
Texture2D arrowNormal=Content.Load(“GUI\\Arrow-Tower\\Arrow-button”);
//箭头按钮的“鼠标悬停”纹理。
Texture2D arrowHover=Content.Load(“GUI\\Arrow-Tower\\Arrow-hover”);
//箭头按钮的“按下”纹理。
Texture2D arrowspressed=Content.Load(“GUI\\Arrow-Tower\\Arrow-pressed”);
//尖峰按钮的“正常”纹理。
Texture2D spikeNormal=Content.Load(“GUI\\Spike-Tower\\Spike-button”);
//spike按钮的“鼠标悬停”纹理。
Texture2D spikeHover=Content.Load(“GUI\\Spike-Tower\\Spike-hover”);
//spike按钮的“按下”纹理。
Texture2D spikePressed=Content.Load(“GUI\\Spike-Tower\\Spike-pressed”);
//慢速按钮的“正常”纹理。
Texture2D slowNormal=Content.Load(“GUI\\Slow-Tower\\Slow-button”);
//慢速按钮的“鼠标悬停”纹理。
Texture2D slowerver=Content.Load(“GUI\\Slow-Tower\\Slow-hover”);
//慢速按钮的“按下”纹理。
Texture2D slowPressed=Content.Load(“GUI\\Slow-Tower\\Slow-pressed”);
//初始化箭头按钮。
arrowButton=新建GUI.Button(箭头正常、箭头悬停、,
按箭头,新矢量2(0,标高高度*32));
//初始化spike按钮。
spikeButton=新GUI.按钮(spikeNormal、spikeHover、,
新矢量2(32,水平高度*32));
//初始化慢速按钮。
slowButton=新建GUI.Button(slowNormal、SlowWhorver、,
慢速压缩,新矢量2(32*2,水平高度*32));
//arrowButton.Clicked+=新事件处理程序(arrowButton\u Clicked);
//spikeButton.Clicked+=新事件处理程序(spikeButton\u Clicked);
//slowButton.Clicked+=新事件处理程序(slowButton\u Clicked);
arrowButton.OnPress+=新的事件处理程序(arrowButton\u OnPress);
spikeButton.OnPress+=新的事件处理程序(spikeButton\u OnPress);
slowButton.OnPress+=新事件处理程序(slowButton\u OnPress);
}
受保护的覆盖无效UnloadContent()
{
}
已单击专用空心箭头按钮(对象发送者,事件参数e)
{
player.NewTowerType=“箭头塔”;
player.NewTowerIndex=0;
}
已单击私有按钮(对象发送者,事件参数e)
{
player.NewTowerType=“斯派克塔”;
player.NewTowerIndex=1;
}
已单击私有无效慢速按钮(对象发送者,事件参数e)
{
player.NewTowerType=“慢塔”;
player.NewTowerIndex=2;
}
私有无效箭头按钮按下(对象发送器,事件参数e)
{
player.NewTowerType=“箭头塔”;
player.NewTowerIndex=0;
}
私有按钮按下(对象发送方,事件参数e)
{
player.NewTowerType=“斯派克塔”;
player.NewTowerIndex=1;
}
私有void slowButton_OnPress(对象发送方,事件参数e)
{
player.NewTowerType=“慢塔”;
player.NewTowerIndex=2;
}
公共向量2大小;
受保护覆盖无效更新(游戏时间游戏时间)
{
MouseState mouse=mouse.GetState(
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);
}
}
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);
}
}
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);