我正在搞一些三角形,並做了這個。起初,我想,我寫的所有「方法」都是實例化的,但是這些「方法」:設備,內容和效果;實際上都是空值。代碼幾乎沒問題,我應該寫些什麼而不是那些?XNA nullreferenceexception,框架類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace Learning_Test1
{
class Triangle
{
GraphicsDevice device;
ContentManager content;
Effect effect;
VertexPositionColor[] vertices;
public void bob()
{
content.RootDirectory = "Content";
content.Load<Effect>("effects");
}
public void Initialize()
{
vertices = new VertexPositionColor[3];
vertices[0].Position = new Vector3(1, 0, 0);
vertices[0].Color = Color.Red;
vertices[1].Position = new Vector3(0, 0, 1);
vertices[1].Color = Color.Green;
vertices[1].Position = new Vector3(-1, 0, 0);
vertices[1].Color = Color.Blue;
}
public void Update()
{
}
public void Draw()
{
effect.CurrentTechnique = effect.Techniques["PretransformedPS"];
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
}
device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1, VertexPositionColor.VertexDeclaration);
}
}
}