XNA 4.0 app closes suddenly when it hits a method

2019-07-11 04:50发布

问题:

As the title says, I have an XNA app that closes suddenly when it hits an app, but doesn't show any errors so I don't have a clue how to even begin to debug it. The code is very simple - I'm just playing with XNA and trying to render a simple triangle - so I can't imagine why it's stopping. The code that's running is

    VertexPositionColor[] vertices;

    public Terrain()
    {
        vertices = new VertexPositionColor[3];
        vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
        vertices[0].Color = Color.Red;
        vertices[1].Position = new Vector3(0, 0.5f, 0f);
        vertices[1].Color = Color.Green;
        vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
        vertices[2].Color = Color.Yellow;
    }

    public void Draw(GameTime gameTime)
    {
        ScreenManager.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
            PrimitiveType.TriangleList,
            vertices,
            0,
            1,
            VertexPositionColor.VertexDeclaration);
    }

and it's the Draw() function that screws it up. When I remove the DrawUserPrimitives line it runs fine (although show nothing...)

回答1:

I'm going to assume that 'ScreenManager' is a class that inherits off 'DrawableGameComponent' and you are accessing the graphics device from there? Make sure somewhere in the constructor of your Game class you are initialising the GraphicsDeviceManager(this).

I think it's the 'GraphicsDevice' that your ScreenManager is grabbing statically that might not be initialised properly.