2011-10-26 59 views
3

我不明白爲什麼會出現這種情況。我從Game類創建一個View類,然後我試圖從Game中的View中調用一個方法,並將整個遊戲對象作爲參數發送給它。如果我將單個變量作爲參數發送,那麼它沒有問題,但我想發送整個Game對象,以便只傳遞一個內容。由於其保護級別,C#無法訪問

遊戲類

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 Airfield 
{ 
    public class Game : Microsoft.Xna.Framework.Game 
    { 
     // Device Objects 
     GraphicsDevice device = null; 
     GraphicsDeviceManager graphics = null; 
     MouseState mouse; 

     // Game Objects 
     SpriteBatch spriteBatch; 
     Texture2D b = null; 
     View view = null; 

     // Arrays 
     Buoy [] buoy = new Buoy[3]; 
     Plane [] plane = new Plane[3]; 

     // Variables 
     bool selected = false; 
     int index = 0; 

     public Game() 
     { 
      graphics = new GraphicsDeviceManager(this); 
     } 

     protected override void Initialize() 
     { 
      Content.RootDirectory = "Content"; 
      this.IsMouseVisible = true; 
      graphics.PreferredBackBufferWidth = 800; 
      graphics.PreferredBackBufferHeight = 600; 
      graphics.ApplyChanges(); 
      base.Initialize(); 

      view = new View(); 

      for (index = 0; index < buoy.Length; index++) 
      { 
       buoy[index] = new Buoy(); 
       plane[index] = buoy[index].CreatePlane(); 
      } 
     } 

     protected override void LoadContent() 
     { 
      device = graphics.GraphicsDevice; 
      spriteBatch = new SpriteBatch(device); 
      b = Content.Load<Texture2D>("buoy"); 
     } 

     protected override void UnloadContent() 
     { 

     } 

     protected override void Update(GameTime gameTime) 
     { 
      base.Update(gameTime); 

      mouse = Mouse.GetState(); 

      if (selected == true || mouse.X >= buoy[0].position.X - 3 && mouse.X <= buoy[0].position.X + 19 && mouse.Y >= buoy[0].position.Y - 3 && mouse.Y <= buoy[0].position.Y + 19) 
      { 
       if (mouse.LeftButton == ButtonState.Pressed) 
       { 
        selected = true; 
        buoy[0].position= new Vector2(mouse.X - 8, mouse.Y - 8); 
       } 

       else 
       { 
        selected = false; 
       } 
      } 
     } 

     protected override void Draw(GameTime gameTime) 
     { 
      base.Draw(gameTime); 
      view.DrawScreen(this); 
     } 
    } 
} 

視圖類

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 Airfield 
{ 
    class View 
    { 
     public View() 
     { 

     } 

     public void DrawScreen(Game game) 
     { 
      game.device.Clear(Color.CornflowerBlue); 

      game.spriteBatch.Begin(); 
      DrawBuoys(game); 
      game.spriteBatch.End(); 
     } 

     public void DrawBuoys(Game game) 
     { 
      for (int x = 0; x < game.buoy.Length; x++) 
      { 
       game.buoy[x].DrawBuoy(game.spriteBatch, game.b); 
      } 
     } 
    } 
} 

基本上我得到每當我嘗試和使用的東西都來自遊戲時間的誤差。

+0

是否通過引用幫助傳遞它? – Tim

回答

5

的這裏的問題是,所有的Game內部成員被標記爲protected或沒有標記在所有默認爲private。這意味着只有Game及其派生的任何類別才能訪問那些protected成員,而不會有其他人訪問privateView類型是完全獨立的類型,因此無法訪問protectedprivate的任何成員。

要公開private字段,他們需要標記爲internalpublic

public class Game : Microsoft.Xna.Framework.Game 
{ 
    ... 
    public SpriteBatch spriteBatch; 
} 

大多數成員也被標註爲override因此你陷入protected。但您可以使用另一個non-protected成員撥打protected。從樣本中不清楚您是否要調用這些方法。

2

所有這一切都暗示private

// Device Objects 
GraphicsDevice device = null; 
GraphicsDeviceManager graphics = null; 
MouseState mouse; 

// Game Objects 
SpriteBatch spriteBatch; 
Texture2D b = null; 
View view = null; 

// Arrays 
Buoy [] buoy = new Buoy[3]; 
Plane [] plane = new Plane[3]; 

// Variables 
bool selected = false; 
int index = 0; 

你需要改變他們public,這是成員變量是一個壞主意。或者,您可以創建屬性來訪問成員變量:

public SpriteBatch SpriteBatch { 
    get { return this.spriteBatch; } 
    protected set { this.spriteBatch = value; } 
} 
+0

它基本上告訴你,這些變量不公開,你試圖像他們那樣訪問它們。 – Yatrix

+1

如果你打算這樣做,那麼考慮減少支持領域。 '公共SpriteBatch SpriteBatch {get;組; }'更短更容易;讓編譯器爲你製作後臺字段。 –

0

默認情況下,在C#中,沒有顯式定義的可訪問性的任何字段都假定爲私有的。所以,你需要做你的Game類以下內容:

// Device Objects 
    public GraphicsDevice device = null; 
    public GraphicsDeviceManager graphics = null; 
    public MouseState mouse; 

    // etc... for your other fields 

其次,值得注意的是,所有的Game類的方法有protected訪問性,這意味着,只有從Game繼承的類就能夠調用這些方法。

相關問題