I was messing around with some triangles and made this. At first, I thought, all the “methods” I wrote were instantiated but those “methods” : device, content and effect; are actually all nulls. The code is almost fine, what was I supposed to write instead of those?
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);
}
}
}
Well the only thing that you are actually initializing is your “vertices” array. I dont see where you actually attempt to initialize the other variables, unless you didnt include some code in your question, for example where you actually call your
You should initialize the rest of your class variables, device, content, and effect in your class’s initialize function unless you plan on setting them elsewhere. It’s been a little while since I last used XNA but if I remember correctly Device and Content might be initialized for you when you create a new game template so all you would have to do is set them equal to testTriangle.Device and testTriangle.Content. If not I would highly recommend looking at the tutorials on the XNA site as they are fantastic and I am 100% sure that you will be able to figure out how to initialize them from there.