C# xna上的错误";由于其保护级别而无法访问”;
我尝试获取鼠标位置时出错 这是我的球员课。我试图让玩家精灵通过鼠标光标移动C# xna上的错误";由于其保护级别而无法访问”;,c#,xna-4.0,C#,Xna 4.0,我尝试获取鼠标位置时出错 这是我的球员课。我试图让玩家精灵通过鼠标光标移动 class Player : Actor { public MouseState mState; EnemyManager enemyManager; public Player(Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, EnemyManager enemyManager) : base(textur
class Player : Actor
{
public MouseState mState;
EnemyManager enemyManager;
public Player(Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, EnemyManager enemyManager)
: base(texture, origin, spriteBatch, new Vector2(250.0f, 516.0f))
{
this.enemyManager = enemyManager;
}
public override void Update(GameTime gameTime)
{
//KeyboardState keyboardState = Keyboard.GetState();
//if (keyboardState.IsKeyDown(Keys.Left))
//{
// if (this.Position.X > 32.0f)
// {
// this.Position -= 10.0f * Vector2.UnitX;
// }
//}
//if (keyboardState.IsKeyDown(Keys.Right))
//{
// if (this.Position.X < 748.0f)
// {
// this.Position += 10.0f * Vector2.UnitX;
// }
//}
MouseState mState = Mouse.GetState();
this.Position = new Vector2(mState.x, mState.y);
// Collisions...
foreach (Enemy e in this.enemyManager.Enemies)
{
if (this.BoundingRectangle.Intersects(e.BoundingRectangle))
{
e.OnHit();
break;
}
}
base.Update(gameTime);
}
}
我的主课
public class MyGame : Microsoft.Xna.Framework.Game
{
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
Actor actor = new Actor();
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);
}
}
公共类MyGame:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
玩家;
灌肠经理灌肠经理;
演员;
公共游戏
{
graphics=新的GraphicsDeviceManager(此);
IsMouseVisible=true;
Content.RootDirectory=“Content”;
}
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
Actor=新Actor();
base.Initialize();
}
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
开始游戏();
}
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
{
这是Exit();
}
this.player.Update(游戏时间);
this.enemyManager.Update(游戏时间);
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:褐红色);
this.spriteBatch.Begin();
这个.player.Draw(游戏时间);
这个.enemyManager.Draw(游戏时间);
this.spriteBatch.End();
基础。抽签(游戏时间);
}
无效开始游戏()
{
enemyManager=新的enemyManager(this.Content.Load(“敌人”)、新矢量2(16)、this.spriteBatch);
player=新玩家(this.Content.Load(“玩家”)、actor.position、this.spriteBatch、enemyManager);
enemyManager。开始游戏(10);
}
}
我认为这一行是问题所在:
this.Position = new Vector2(mState.x, mState.y);
x
和y
未在MouseState
上公开。您需要将它们大写(C#区分大小写)。改用这个:
this.Position = new Vector2(mState.X, mState.Y);
“进行这些更改会给我一个不同的错误对象引用,该引用未设置为player=new player(this.Content.Load(“player”)、actor.position、this.spriteBatch、enemyManager行中的对象实例);在我的主类上,它表示错误‘Shmup.actor’不包含接受0个参数的构造函数–” Actor的构造函数需要-Texture2D纹理、Vector2原点、SpriteBatch SpriteBatch、Vector2初始位置 因此,在提供声明新参与者所需的所有信息后,需要将“Actor=new Actor();”更改为LoadContent()或单独的方法 它应该阅读以下内容(用游戏类中的适当项替换每个参数): 演员=新演员(纹理、原点、spriteBatch、初始位置)
如果您这样做,它将修复错误,如果您不这样做,您将继续得到错误,除非您在Actor中定义了一个接受0个参数的构造函数。希望这能有所帮助。错误发生在哪一行?异常发生在哪里?它说什么是不可访问的?this.Position中的错误=新矢量2(mState.x,mState.y);行,并说x和y不可访问进行这些更改会给我一个不同的错误对象引用,该引用未设置为行中对象的实例player=new player(this.Content.Load(“player”)、actor.position、this.spriteBatch、enemyManager);在我的主类上,它说Error'Shmup.Actor'不包含接受0参数的构造函数同一行上的不同错误或其他地方的不同错误?您可能在其他地方有其他编译器错误。在其他地方有不同的错误我将我的主类放在player=new player(this.Content.Load(“player”)、actor.position、this.spriteBatch、enemyManager)行中的new error accurs处;
this.Position = new Vector2(mState.X, mState.Y);