C# Xna SpriteBatch矩阵不会将精灵绘制到屏幕
我目前对我的Xna游戏1课程有一个问题。我已经将一个2D相机与它自己的独立类合并在一起,该类被称为矩阵的使用。我遇到的问题是draw方法,我使用了 : `spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend,null,null,null,camera.TransformMatrix) 问题发生在案例方法更改绘制内容之后,例如,案例主菜单和播放似乎无法到达,或者坦白地说,无法绘制到屏幕上。我尝试在每个案例中放置一个单独的SpriteBatch.Begin方法,因此,在案例中使用SpriteBatch.end()结束每个方法本身 我也尝试过,没有在update方法中调用translateCamera类,但也没有用 任何帮助都将不胜感激C# Xna SpriteBatch矩阵不会将精灵绘制到屏幕,c#,matrix,xna,C#,Matrix,Xna,我目前对我的Xna游戏1课程有一个问题。我已经将一个2D相机与它自己的独立类合并在一起,该类被称为矩阵的使用。我遇到的问题是draw方法,我使用了 : `spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend,null,null,null,camera.TransformMatrix) 问题发生在案例方法更改绘制内容之后,例如,案例主菜单和播放似乎无法到达,或者坦白地说,无法绘制到屏幕上。我尝试在每个案例中放置一个单独
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
screenRectangle = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
npc_creation = new NPC_Creation[0];
player1 = new charDevelopment();
camera = new Camera2D();
}
/// <summary>
protected override void Initialize()
{
Random rnd = new Random();
rndYpos = rnd.Next(200,500);
rndXpos = rnd.Next(100, 300);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
protected override void LoadContent()
{
#region loadTextures
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
backGround = Content.Load<Texture2D>("MainMenu");
graphics.PreferredBackBufferWidth = screenWidth; // width of screen..
graphics.PreferredBackBufferHeight = screenHeight; //height of screen..
IsMouseVisible = true;
graphics.ApplyChanges(); // load images...
potion = Content.Load<Texture2D>("potion");
itemrect = new Rectangle(((screenRectangle.Width + potion.Width) / 2), ((screenRectangle.Height + potion.Height) / 2), potion.Width / 4, potion.Height / 4);
item = new ItemPotion(potion, itemrect, screenRectangle);
OnScreenLevel = Content.Load<SpriteFont>("OnScreenLevel");
charAtkSound = Content.Load<SoundEffect>("AttackSound"); //loads attack sound
Grave = Content.Load<Texture2D>("Grave");
HealthBar = Content.Load<Texture2D>("HealthBar");
btnPlay = new cButton(Content.Load<Texture2D>("Button1"), graphics.GraphicsDevice);
movement = new cMovement(Content.Load<Texture2D>("SpritesRe"), new Vector2(rndXpos, rndYpos), 64, 57, HealthBar, cBar,Grave);
Inv = new InventoryScreen(movement);
TempNPC = Content.Load<Texture2D>("MonsterSprite");
//npc_creation[i] = new NPC_Creation(Content.Load<Texture2D>("MonsterSprite"), screenRectangle,HealthBar,Content.Load<Texture2D>("Grave"));
btnPlay.setPosition(new Vector2(350, 300));
StartGame();
#endregion
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState();
TranslateCamera(keyState);
//Spawns the mobs
if (map.getSpawnStatus() == true)
{
npc_creation = new NPC_Creation[map.getNumOfNPC()];
for (int i = 0; i < npc_creation.Length; i++)
{
npc_creation[i] = new NPC_Creation(TempNPC, screenRectangle, HealthBar, Grave);
npc_creation[i].setSpawnPosition(-10*i);
}
map.setSpawnStatus(false);
}
//888888
MouseState mouse = Mouse.GetState();
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
switch (CurrentGameState)
{
case GameState.MainMenu: // if mouse is clicked... call method
if (btnPlay.isClicked == true) CurrentGameState = GameState.charCreate;
btnPlay.Update(mouse);
break;
case GameState.charCreate: // creating character
charCreate.Show(); // show menu
if (charCreate.create == true)
{
charCreate.Close(); // close menu
CurrentGameState = GameState.Playing;
}
break;
case GameState.Playing: // if true.. load new image...
backGround = Content.Load<Texture2D>("Game Map");
if (movement.Alive == false)
{
CurrentGameState = GameState.GameOver;
}
else if (Keyboard.GetState().IsKeyDown(Keys.E))
{
Inv.Show();
}
else if (Keyboard.GetState().IsKeyDown(Keys.Q))
{
Inv.Hide();
}
break;
case GameState.GameOver: // end game screen
end.Show(); // pop up screen
if (end.retry == true) // if click retry
{
Application.Restart(); // restart program
break;
}
if (end.quit == true) // if click quit
{
this.Exit(); // quit
break;
}
break;
}
if (movement.getAttackSound() == true)
{
charAtkSound.Play();
}
for (int i = 0; i < npc_creation.Length; i++)
{
//checks if character takes damage from being in range of enemy
npc_creation[i].charWithinDamageRange(movement.getCharLocation());
//checks if character is within sight range
npc_creation[i].charWithinRange(movement.getCharLocation());
//calls on monster update method, with the inputs:
npc_creation[i].Update(gameTime, npc_creation[i].withinRangeOrNot(), movement.getCharLocation(), i);
//checks if monster is in range of character attack
npc_creation[i].monsterTakeDamageRange(movement.getAttackRadius());
npc_creation[i].charWithinDamageRange(movement.getCharLocation());
//*************************************************************Added Jan 9th*****************
//checks if monster is dead, and once dead to give character EXP from mob once
if ((npc_creation[i].deadOrAlive() == false)&&(npc_creation[i].getEXPgivenOrNot() == false))
{
//character gains exp determined by returnEXPworth
player1.EXPgained(npc_creation[i].returnEXPworth());
//tells program that exp has been gained by character
npc_creation[i].setEXPgiven();
//*****BELOW PRINTS OUT CURRENT EXP, GAME MUST BE SET TO CONSOLE MODE****
Console.WriteLine("Current char EXP is "+player1.getEXP());
}
//**************************************************************Added Jan 9th****************
if (npc_creation[i].withinDamageRangeOrNot() == true)
{
movement.decreaseCharHealth();
npc_creation[i].charWithinDamageRange(movement.getCharLocation());
npc_creation[i].withinDamageRangeOrNot();
}
while (npc_creation[i].inAttackRadius() == true)
{
npc_creation[i].decreaseMobHealth(item);
movement.resetAttackRadius();
npc_creation[i].monsterTakeDamageRange(movement.getAttackRadius());
npc_creation[i].inAttackRadius();
}
movement.Update(gameTime, map);
}
item.Update(movement.getCharLocation(), Inv);
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// do not erase comments! added SUnday
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.TransformMatrix);
//spriteBatch.Begin();
spriteBatch.Draw(backGround, Vector2.Zero, Color.White);
switch (CurrentGameState)
{
case GameState.MainMenu: // if on main menu... draw button...
btnPlay.Draw(spriteBatch);
break;
case GameState.Playing: // otherwise.. draw the character sprites
movement.Draw(spriteBatch);
player1.checkLevel();
spriteBatch.DrawString(OnScreenLevel, "Current Level: " + player1.getLvL() +"\nUserName: " +charCreate.getInput(), new Vector2(0, 5), Color.White);
for (int i = 0; i < npc_creation.Length; i++)
{
npc_creation[i].Draw(spriteBatch, Color.White);
}
item.Draw(spriteBatch);
break;
}
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
private void TranslateCamera(KeyboardState keyState)
{
Vector2 cameraTranslate = Vector2.Zero;
if (keyState.IsKeyDown(Keys.W))
cameraTranslate.Y =1;
cameraTranslate.X = 0;
if (keyState.IsKeyDown(Keys.A))
cameraTranslate.X =-1;
cameraTranslate.Y = 0;
if (keyState.IsKeyDown(Keys.D))
cameraTranslate.X =1;
cameraTranslate.Y = 0;
if (keyState.IsKeyDown(Keys.S))
cameraTranslate.Y =-1;
cameraTranslate.X = 0;
camera.Position += cameraTranslate;
}
private void StartGame()
{
map = new Map(backGround, movement);
item.spawnLocation();
for (int i = 0; i < npc_creation.Length; i++)
{
npc_creation[i].setSpawnPosition(i);
}
}
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferWidth=800;
graphics.PreferredBackBufferHeight=600;
screenRectangle=新矩形(0,0,graphics.PreferredBackBufferWidth,graphics.PreferredBackBufferHeight);
npc_创建=新建npc_创建[0];
player1=新开发();
摄像机=新摄像机2D();
}
///
受保护的覆盖无效初始化()
{
随机rnd=新随机();
rndYpos=rnd.Next(200500);
rndXpos=rnd.Next(100300);
base.Initialize();
}
///
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
受保护的覆盖void LoadContent()
{
#区域加载纹理
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
后台=Content.Load(“主菜单”);
graphics.PreferredBackBufferWidth=screenWidth;//屏幕宽度。。
graphics.PreferredBackBufferHeight=屏幕高度;//屏幕高度。。
IsMouseVisible=true;
graphics.ApplyChanges();//加载图像。。。
药水=内容量(“药水”);
itemrect=新矩形((screenRectangle.Width+potion.Width)/2),(screenRectangle.Height+potion.Height)/2),potion.Width/4,potion.Height/4);
项目=新项目药剂(药剂,项目矩形,屏幕矩形);
OnScreenLevel=Content.Load(“OnScreenLevel”);
charAtkSound=Content.Load(“AttackSound”);//加载攻击声音
坟墓=内容。负载(“坟墓”);
HealthBar=Content.Load(“HealthBar”);
btnPlay=新的cButton(Content.Load(“Button1”)、graphics.GraphicsDevice;
移动=新的cMovement(Content.Load(“精灵”)、新矢量2(rndXpos、rndYpos)、64、57、HealthBar、cBar、Grave);
Inv=新库存屏幕(移动);
TempNPC=Content.Load(“怪物精灵”);
//npc_创建[i]=新的npc_创建(Content.Load(“怪物精灵”)、屏幕矩形、健康栏、Content.Load(“坟墓”);
BTN显示设置位置(新矢量2(350300));
StartGame();
#端区
}
///
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
///
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
受保护覆盖无效更新(游戏时间游戏时间)
{
KeyboardState keyState=Keyboard.GetState();
TranslateCamera(键状态);
//滋生暴徒
if(map.getSpawnStatus()==true)
{
npc_creation=新建npc_creation[map.getNumOfNPC()];
for(int i=0;i