问题
SomeClass.cs
public void clearscreen()
{
main.GraphicsDevice.Clear(Color.Black);
}
Why can't I clear the screen by calling on this method from another class?
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
mainMenu.MenuDraw();
spriteBatch.Draw(cursorTexture, cursorPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
The clearscreen method is being called from within mainMenu.Draw();
回答1:
You need to pass your GraphicsDeviceManager (most likely named 'graphics') to the class and call it as so.
graphics.GraphicsDevice.Clear(Color.Black);
回答2:
Use this in your Draw method:
protected override void Draw(GameTime cGameTime)
{
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
....
....
....
回答3:
If you redirect your drawing method to another class you still need to use the first graphicsDevice as declared in Game1.cs (a default class name for new MonoGame project). My approach was to create another class that would hold the graphicsDevice reference in static public access. Therefore:
Game1.cs:LoadContent
spriteBatch = new SpriteBatch(GraphicsDevice);
Memory.spriteBatch = spriteBatch;
Memory.cs:
class Memory
{
//monogame
public static GraphicsDeviceManager graphics;
public static SpriteBatch spriteBatch;
Now in your redirected class, e.g. BattleModule.cs:
private static void DrawGeometry()
{
Memory.spriteBatch.GraphicsDevice.Clear(Color.Black);
来源:https://stackoverflow.com/questions/9451112/clearing-the-screen