Clearing the screen

ε祈祈猫儿з 提交于 2019-12-11 04:41:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!