C# 我得到一个空错误
我在这行收到一条错误消息:C# 我得到一个空错误,c#,xna,xna-4.0,C#,Xna,Xna 4.0,我在这行收到一条错误消息: player = new Player(this.Content.Load<Texture2D>("Player"), actor.position, this.spriteBatch, enemyManager); 正如错误所说,您需要创建Actor类的实例 在游戏构造函数或StartGame方法中(在创建Player类实例之前
player = new Player(this.Content.Load<Texture2D>("Player"),
actor.position,
this.spriteBatch,
enemyManager);
正如错误所说,您需要创建Actor类的实例 在游戏构造函数或StartGame方法中(在创建Player类实例之前),您应该创建Actor类的实例,如:
this.actor=新的actor(变量…) 那么,为什么要使用
actor.position
呢?如果您从未向actor
提供值,那么您似乎没有创建actor对象;在start中,游戏函数首先初始化演员。你还需要那个演员吗?也许可以创建一个固定位置的玩家,比如为敌人?我正在尝试使用鼠标位置,所以它不能是constantI假设如果OP理解错误消息所说的内容,他/她不会询问。我建议您在回答中添加一个示例或解释,否则它也可能是一个注释;
class MyGame // [edit:] added based on below constructor
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player player;
EnemyManager enemyManager;
Actor actor;
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
IsMouseVisible = true;
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
startTheGame();
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
this.player.Update(gameTime);
this.enemyManager.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Maroon);
this.spriteBatch.Begin();
this.player.Draw(gameTime);
this.enemyManager.Draw(gameTime);
this.spriteBatch.End();
base.Draw(gameTime);
}
void startTheGame()
{
enemyManager = new EnemyManager(this.Content.Load<Texture2D>("Enemy"), new Vector2(16), this.spriteBatch);
player = new Player(this.Content.Load<Texture2D>("Player"),actor.position, this.spriteBatch, enemyManager);
enemyManager.StartTheGame(10);
}
}
public class Actor
{
public Texture2D texture;
public Vector2 origin;
public SpriteBatch spriteBatch;
public Vector2 position;
public Rectangle boundingRectangle;
public Vector2 Position
{
get { return position; }
set
{
position = value;
boundingRectangle = new Rectangle((Int32)(position.X - origin.X), (Int32)(position.Y - origin.Y), texture.Width, texture.Height);
}
}
public Rectangle BoundingRectangle
{
get { return boundingRectangle; }
}
public Actor(Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, Vector2 initialPosition)
{
this.texture = texture;
this.origin = origin;
this.spriteBatch = spriteBatch;
this.position = initialPosition;
boundingRectangle = new Rectangle((Int32)(initialPosition.X - origin.X), (Int32)(initialPosition.Y - origin.Y), texture.Width, texture.Height);
}
public virtual void Update(GameTime gameTime)
{ }
public virtual void Draw(GameTime gameTime)
{
this.spriteBatch.Draw(texture, position - origin, Color.White);
}
}