xna で c# の学習を始めたばかりで、単純なタワー ディフェンス ゲームを作成しています。行を追加すると、エラー メッセージが表示されます。
graphic.ApplyChanges();
エラー メッセージのスクリーン ショットへのリンクを次に示します。ゲームを開始すると、数秒間実行されてから終了し、このメッセージが表示されます。このゲームの前にいくつかのゲームを作成しましたが、これまでこの問題が発生したことはありません。私はプログラミングの初心者なので、他に知っておくべきことがあれば教えてください。
更新: 新しいプロジェクトでエラーを再現しようとしましたが、成功しませんでした。ここにコードを投稿して、私よりも知識のある人がコードを読んで、エラーの原因を見つけられるようにします。
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);
}
}
}
}
}