我刚刚开始学习xna中的c#,我正在制作一个简单的塔防游戏。当我添加这一行时,我收到一条错误消息。
graphic.ApplyChanges();Here是指向错误消息屏幕截图的链接。当我启动游戏时,它会运行几秒钟,然后关闭,并向我显示以下消息。在这个之前,我已经做了一些其他的游戏,我以前从来没有遇到过这个问题。我是编程的初学者,所以如果你还有什么需要帮助的,请让我知道。
更新:我尝试在一个新项目中重现这个错误,但没有成功。我将在这里发布我的代码,这样比我知识更多的人可以阅读它,也许可以找到导致错误的原因。
Game1类:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace AdventureGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D tilegraphic;
MouseState oldmouse;
MouseState currentmouse;
Point mouseposition;
Color defaultcolor;
Color selectedcolor;
Color pathcolor;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
IsMouseVisible = true;
// TODO: Add your initialization logic here
base.Initialize();
oldmouse = new MouseState();
currentmouse = new MouseState();
defaultcolor = new Color();
selectedcolor = new Color();
defaultcolor = Color.LawnGreen;
selectedcolor = Color.DarkGreen;
pathcolor = Color.SaddleBrown;
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
tilegraphic = Content.Load<Texture2D>("tilegraphic") as Texture2D;
// TODO: use this.Content to load your game content here
}
/// <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
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
oldmouse = currentmouse;
currentmouse = Mouse.GetState();
mouseposition = new Point(currentmouse.X, currentmouse.Y);
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
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.White);
spriteBatch.Begin();
DrawMap();
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
public bool MouseJustPressed()
{
if (oldmouse.LeftButton == ButtonState.Released && currentmouse.LeftButton == ButtonState.Pressed)
{
return true;
}
else
{
return false;
}
}
public void DrawMap()
{
Map1 map = new Map1();
map.Layout();
map.CreateRectangles();
for (int x = 0; x < map.mapwidth; x++)
{
for (int y = 0; y < map.mapheight; y++)
{
switch (map.tiles[x, y])
{
case 0:
if (map.tilerectangles[x, y].Contains(mouseposition))
{
spriteBatch.Draw(tilegraphic, map.tilerectangles[x, y], selectedcolor);
}
else
{
spriteBatch.Draw(tilegraphic, map.tilerectangles[x, y], defaultcolor);
}
break;
case 1:
spriteBatch.Draw(tilegraphic, map.tilerectangles[x, y], pathcolor);
break;
}
}
}
}
}
}Map1类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace AdventureGame
{
class Map1
{
public int[,] tiles = new int[7, 7];
public Rectangle[,] tilerectangles = new Rectangle[8, 8];
Game1 game = new Game1();
public int mapwidth = 8;
public int mapheight = 8;
public void Layout()
{
tiles = new int[,] {
{0,1,0,0,0,0,0,0},
{0,1,0,1,1,1,1,1},
{0,1,0,1,0,0,0,0},
{0,1,0,1,1,1,1,0},
{0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0},};
}
public void CreateRectangles()
{
for (int x = 0; x < mapwidth; x++)
{
for (int y = 0; y < mapheight; y++)
{
tilerectangles[x, y] = new Rectangle(x * 70, y * 70, 70, 70);
}
}
}
}
}发布于 2012-11-05 23:15:52
我已经找到了代码中的错误之处。当我在我的方法DrawMap()中添加行Game1 game = new Game1();来快速测试它到目前为止是否正常工作时,程序显然不喜欢它,并给了我一个错误。因此,当我用我的其他变量声明它,并将行map = new Map1();放入initialize方法中时,它工作得很好。谢谢你给我的帮助。
发布于 2012-11-04 03:22:02
在Initialize()方法中,将行Graphics.ApplyChanges()放在base.Initialize()行之后,或放在LoadContent()方法的开头。
https://stackoverflow.com/questions/13211896
复制相似问题