Ric*_*rdG 3 c# model-view-controller xna
我无法弄清楚为什么会这样.我正在从Game类创建一个View类,然后我尝试从View in Game中调用一个方法,并将整个游戏对象作为参数发送给它.如果我发送单个变量作为参数然后它没有问题,但我想发送整个Game对象,以便只传递一件事.
游戏课
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
查看课程
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我每次尝试使用来自游戏的东西时都会收到错误.
这里的问题是内部的所有成员Game都被标记为protected或者根本没有标记为默认值private.这意味着只有Game从它派生的任何类都可以访问这些protected成员而不能访问其他任何成员private.类型View是完全独立的类型,因此无法访问任何成员protected或private.
要公开private字段,需要将它们标记为internal或public.
public class Game : Microsoft.Xna.Framework.Game
{
...
public SpriteBatch spriteBatch;
}
Run Code Online (Sandbox Code Playgroud)
这些成员中的大多数也被标记为override因此您被锁定protected.但是你可以使用另一个non-protected成员来调用protected.从样本中不清楚是否要调用这些方法.