2012-08-29 99 views
-1

當我嘗試獲取鼠標位置時出現錯誤。由於其保護級別,xna「無法訪問」

這是我的球員類。我試圖讓鼠標的玩家精靈移動光標

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); 
    } 
} 

,這是我Actor類具有位置變量

namespace Shmup 
{ 
    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; } 
     } 


     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); 
     } 
    } 
} 

我的主類

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); 
    } 
} 
+0

發生什麼錯誤? – SomeWritesReserved

+0

異常在哪裏發生?它說什麼是不可訪問的? –

+0

錯誤在this.Position = new Vector2(mState.x,mState.y);並且說x和y不可訪問 –

回答

1

這行是問題我認爲:

this.Position = new Vector2(mState.x, mState.y); 

xy未在MouseState上公開曝光。您需要將它們大寫(C#區分大小寫)。使用這個來代替:

this.Position = new Vector2(mState.X, mState.Y); 
+0

使這些更改給我一個不同的錯誤 未將對象引用設置爲行中對象的實例 player = new Player(this.Content.Load (「Player」), actor.position,this.spriteBatch,enemyManager); 它說我的主類錯誤\t'Shmup.Actor'不包含一個構造函數,它需要0個參數 –

+0

在同一行上的不同錯誤或其他地方的另一個錯誤?其他地方可能有其他編譯器錯誤。 – SomeWritesReserved

+0

不同的錯誤在其他地方 我把我的主課堂,新的錯誤accurs在該行 播放器=新播放器(this.Content.Load (「玩家」),actor.position,this.spriteBatch,enemyManager ); –

0

「做了這些改變讓我沒有設置爲在該行的球員=新播放器(this.Content.Load(一個對象的實例不同的錯誤對象引用」播放器「),演員.position,this.spriteBatch,enemyManager);在我的主類上它說錯誤'Shmup.Actor'不包含一個構造函數,它需要0個參數 - 「

Actor的構造函數需要 - Texture2D紋理,Vector2原點,SpriteBatch spriteBatch,Vector2 initialPosition。

所以你需要改變「演員actor =新的演員();」在提供聲明新Actor所需的所有信息後,將它們轉換爲LoadContent()或單獨的方法。

應該讀東西沿着這些路線(從你的遊戲類取代適當的項目每個參數):

演員演員=新演員(質地,產地,spriteBatch,initialPosition);

如果你這樣做,它會解決錯誤,如果你不這樣做,你會繼續得到錯誤,除非你在Actor中定義了一個構造函數,它帶有0個參數。希望這有些幫助。

相關問題