Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加线条图形时的NoSuitableGraphicsDeviceException.ApplyChanges();_C#_Xna - Fatal编程技术网

C# 添加线条图形时的NoSuitableGraphicsDeviceException.ApplyChanges();

C# 添加线条图形时的NoSuitableGraphicsDeviceException.ApplyChanges();,c#,xna,C#,Xna,我刚刚开始在xna学习c#,我正在制作一个简单的塔防游戏。我在添加行时收到一条错误消息 graphic.ApplyChanges(); 是指向错误消息屏幕截图的链接。 当我开始游戏时,它会运行几秒钟,然后关闭并向我显示此消息。 在这之前我已经打了几场比赛,我从来没有遇到过这个问题。我是一个编程新手,所以如果你还需要知道什么来帮助我,请告诉我 更新: 我曾试图在一个新项目中重现这个错误,但没有成功。我将在这里发布我的代码,这样比我更有知识的人可以阅读它,也许可以找到导致错误的原因 Game1类:

我刚刚开始在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;
                }


            }   

        }
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
命名空间冒险游戏
{
/// 
///这是游戏的主要类型
/// 
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧;
纹理2维瓦楞;
老鼠;老老鼠;
鼠标;鼠标;
点鼠标定位;
颜色默认颜色;
颜色选择颜色;
颜色路径颜色;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferWidth=800;
graphics.PreferredBackBufferHeight=600;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
IsMouseVisible=true;
//TODO:在此处添加初始化逻辑
base.Initialize();
oldmouse=新的MouseState();
currentmouse=newmousestate();
defaultcolor=新颜色();
selectedcolor=新颜色();
defaultcolor=Color.LawnGreen;
selectedcolor=Color.DarkGreen;
pathcolor=Color.SaddleBrown;
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
tilegraphic=Content.Load(“tilegraphic”)作为纹理2d;
//TODO:使用此.Content在此处加载游戏内容
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
oldmouse=currentmouse;
currentmouse=Mouse.GetState();
mouseposition=新点(currentmouse.X,currentmouse.Y);
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
//TODO:在此处添加更新逻辑
更新(游戏时间);
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色。白色);
spriteBatch.Begin();
DrawMap();
spriteBatch.End();
//TODO:在此处添加图形代码
基础。抽签(游戏时间);
}
公共bool MouseJustPressed()
{
如果(oldmouse.LeftButton==ButtonState.Released&¤tmouse.LeftButton==ButtonState.Pressed)
{
返回true;
}
其他的
{
返回false;
}
}
公共地图()
{
Map1 map=新的Map1();
map.Layout();
CreateRectangles();
对于(int x=0;x
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);
            }

        }




    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
命名空间冒险游戏
{
类映射1
{
公共int[,]tiles=新int[7,7];
公共矩形[,]平铺矩形=新矩形[8,8];
Game1 game=新Game1();
公共int mapwidth=8;
公共地图高度=8;
聚氨基甲酸酯