C# xna 4.0加载纹理2D时出错 使用系统; 使用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 { 图形管理器图形; SpriteBatch SpriteBatch; 纹理2d-myTexture; 矩形; 公共游戏1() { graphics=新的GraphicsDeviceManager(此); Content.RootDirectory=“Content”; } /// ///允许游戏在开始运行之前执行任何需要的初始化。 ///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务 ///相关内容。调用base.Initialize将枚举所有组件 ///并对它们进行初始化。 /// 受保护的覆盖无效初始化() { //TODO:在此处添加初始化逻辑 base.Initialize(); } /// ///LoadContent将在每个游戏中调用一次,并且是加载的地方 ///你所有的内容。 /// 受保护的覆盖void LoadContent() { //创建一个新的SpriteBatch,可用于绘制纹理。 //spriteBatch=新spriteBatch(图形设备); 纹理myTexture=Content.Load(“character.png”); myRectangle=新矩形(10100,30,50); //TODO:使用此.Content在此处加载游戏内容 } /// ///UnloadContent将在每个游戏中调用一次,并且是卸载的地方 ///所有内容。 /// 受保护的覆盖无效UnloadContent() { //TODO:在此卸载任何非ContentManager内容 } /// ///允许游戏运行逻辑,例如更新世界, ///检查碰撞、收集输入和播放音频。 /// ///提供计时值的快照。 受保护覆盖无效更新(游戏时间游戏时间) { //允许游戏退出 if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed) 这是Exit(); //TODO:在此处添加更新逻辑 更新(游戏时间); } /// ///这就是所谓的比赛应该平局的时候。 /// ///提供计时值的快照。 受保护覆盖无效绘制(游戏时间游戏时间) { 图形设备。清晰(颜色:矢车菊蓝); //TODO:在此处添加图形代码 spriteBatch.Begin(); 绘制(myTexture、myRectangle、Color.White); spriteBatch.End(); 基础。抽签(游戏时间); } } }
这其中的错误是什么?这将返回加载“character.png”时的错误。找不到文件。 我已经创建了character.png并将其添加到我的项目中,但它也不起作用 我使用visual studio 2010 c#with xna 4.0试试这个:C# xna 4.0加载纹理2D时出错 使用系统; 使用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 { 图形管理器图形; SpriteBatch SpriteBatch; 纹理2d-myTexture; 矩形; 公共游戏1() { graphics=新的GraphicsDeviceManager(此); Content.RootDirectory=“Content”; } /// ///允许游戏在开始运行之前执行任何需要的初始化。 ///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务 ///相关内容。调用base.Initialize将枚举所有组件 ///并对它们进行初始化。 /// 受保护的覆盖无效初始化() { //TODO:在此处添加初始化逻辑 base.Initialize(); } /// ///LoadContent将在每个游戏中调用一次,并且是加载的地方 ///你所有的内容。 /// 受保护的覆盖void LoadContent() { //创建一个新的SpriteBatch,可用于绘制纹理。 //spriteBatch=新spriteBatch(图形设备); 纹理myTexture=Content.Load(“character.png”); myRectangle=新矩形(10100,30,50); //TODO:使用此.Content在此处加载游戏内容 } /// ///UnloadContent将在每个游戏中调用一次,并且是卸载的地方 ///所有内容。 /// 受保护的覆盖无效UnloadContent() { //TODO:在此卸载任何非ContentManager内容 } /// ///允许游戏运行逻辑,例如更新世界, ///检查碰撞、收集输入和播放音频。 /// ///提供计时值的快照。 受保护覆盖无效更新(游戏时间游戏时间) { //允许游戏退出 if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed) 这是Exit(); //TODO:在此处添加更新逻辑 更新(游戏时间); } /// ///这就是所谓的比赛应该平局的时候。 /// ///提供计时值的快照。 受保护覆盖无效绘制(游戏时间游戏时间) { 图形设备。清晰(颜色:矢车菊蓝); //TODO:在此处添加图形代码 spriteBatch.Begin(); 绘制(myTexture、myRectangle、Color.White); spriteBatch.End(); 基础。抽签(游戏时间); } } },c#,visual-studio-2010,xna,xna-4.0,C#,Visual Studio 2010,Xna,Xna 4.0,这其中的错误是什么?这将返回加载“character.png”时的错误。找不到文件。 我已经创建了character.png并将其添加到我的项目中,但它也不起作用 我使用visual studio 2010 c#with xna 4.0试试这个: using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framewor
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 Project
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D myTexture;
Rectangle myRectangle;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <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()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <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);
Texture myTexture = Content.Load<Texture2D>("character.png");
myRectangle = new Rectangle(10, 100, 30, 50);
// 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)
{
// 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.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(myTexture,myRectangle,Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
myTexture=Content.Load(“字符”);
编辑:如果仍不起作用,请确保将文件设置为使用正确的内容导入器,如下所述:。执行以下操作:
myTexture = Content.Load<Texture2D>("character");
Texture myTexture=Content.Load(“字符”);
它为什么起作用?Load方法接受资产名称,而不是文件路径。您需要在解决方案资源管理器中单击资产,然后找到资产名称。这是加载方法的参数。
希望这对你有帮助,祝你将来好运 转到文件的属性,确保内容导入器设置为Texture-XNA Framework,内容处理器设置为Texture-XNA Framework。离开LoadContent方法时,myTexture将超出范围。myTexture需要是类级变量。@MattDavey谢谢,我回答问题时没听清楚。
Texture myTexture = Content.Load<Texture2D>("character");