Error creating a hitbox? - c#

So I have a HealthPickup class and a Player class. Each class has this line of code:
public static Rectangle Hitbox = new Rectangle(Pos.X, Pos.Y, Tex.Width, Tex.Height);
My question is, why is there an error on the Player class and not the HealthPickup class? Edit: If i step over the Player hitbox, the HealthPickup causes the same error.Is it something to do with my Rectangle? The error is a TypeInitializationException which details follow as such:
System.TypeInitializationException was unhandled
HResult=-2146233036
Message=The type initializer for 'TestGame.Player' threw an exception.
Source=TestGame
TypeName=TestGame.Player
StackTrace:
at TestGame.RPG.LoadContent() in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\RPG.cs:line 41
at Microsoft.Xna.Framework.Game.Initialize()
at TestGame.RPG.Initialize() in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\RPG.cs:line 33
at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
at Microsoft.Xna.Framework.Game.Run()
at TestGame.Program.Main(String[] args) in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\Program.cs:line 15
InnerException: System.NullReferenceException
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=TestGame
StackTrace:
at TestGame.Player..cctor() in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\Player.cs:line 20
InnerException:
Edit: I stopped the error but I still need collision-detection, I'm using Console.Beep() to determine whether code runs or not. And, as per request, more code :)
public class HealthPickup : Microsoft.Xna.Framework.GameComponent
{
public static Texture2D Tex;
public static Point Pos = new Point(50, 50);
public static Rectangle Hitbox = new Rectangle(Pos.X, Pos.Y, 32, 32);
public HealthPickup(Game game) : base(game) { }
public override void Initialize() { base.Initialize(); }
public override void Update(GameTime gameTime)
{
//if (Tex.Bounds.Intersects(Player.Tex.Bounds)) //was testing other collision-detection methods
//{
// Console.Beep(); //the only way I can check if it's being run or not
//}
}
}
In Draw():
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
// Create Useless Rectangles
Rectangle player = new Rectangle(Player.Pos.X, Player.Pos.Y, Player.Tex.Width, Player.Tex.Height);
Rectangle healthPickup = new Rectangle(HealthPickup.Pos.X, HealthPickup.Pos.Y, HealthPickup.Tex.Width, HealthPickup.Tex.Height);
// Draw Sprites
spriteBatch.Draw(Player.Tex, player, Color.White);
spriteBatch.Draw(HealthPickup.Tex, healthPickup, Color.White);
spriteBatch.End();
base.Draw(gameTime);
Player class:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace TestGame
{
public class Player : Microsoft.Xna.Framework.GameComponent
{
public static Texture2D Tex;
public static string Dir = "D";
public static Point Pos = new Point(GraphicsDeviceManager.DefaultBackBufferWidth / 2, GraphicsDeviceManager.DefaultBackBufferHeight / 2);
public static int speed = 4;
public static Rectangle Hitbox = new Rectangle(Pos.X, Pos.Y, Tex.Width, Tex.Height);
public Player(Game game): base(game) { }
public override void Initialize() { base.Initialize(); }
public override void Update(GameTime gameTime) { }
}
}

This exception is thrown whenever a static constructor throws an exception, and in this case it seems like you have a NullReferenceException in the static constructor of the Player class.
EDIT:
The problem here is the Tex static field which isn't initialized before HitBox is created, and this explains the reason of the NullReferenceExcpetion that you get. So in order to solve the problem you have to initialize it, and you can do it in this way in the Player class:
public static Rectangle HitBox;
//other fields and methods
public override void LoadContent(){
ContentManager content = MyGame.ContentManager;
Tex = content.Load<Texture2D>("mytexture");
//...
base.LoadContent();
this.InitializeAfterLoadingContent();
}
private void InitializeAfterLoadingContent(){
Hitbox = new Rectangle(Pos.X, Pos.Y, Tex.Width, Tex.Height);
}
As you can see I created another method cause a problem comes when the graphics resources are needed to initialize. In XNA the Initialize method is called before the LoadContent, so what you need to overcome this problem is to create a method and call it after loading content. Also your Game class can be done as:
public class MyGame : Microsoft.Xna.Framework.Game {
public static ContentManager ContentManager;
//...
public MyGame( ) {
//...
ContentManager = Content;
}
protected override void Initialize(){
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
//...
this.InitializeAfterLoadingContent();
}
//you can call it however you want
private void InitializeAfterLoadingContent(){
//initialize your non-graphical resources by using the content info you need,
//like the width and height of a texture you loaded
}
//...
}

Related

Drawing a Sprite using Monogame

I'm just starting with Monogame and I'm trying to make a simple sprite, which later is meant to be a button. I've searched all around and done several tutorials, but I can't make it work. I just keep getting the blank, blue screen. Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace Test_Game
{
class Main_Menu
{
//setting the variables
public Texture2D button1;
public Vector2 button1Pos;
public GraphicsDevice graphicsDevice;
GraphicsDeviceManager graphics;
public void initialize(Texture2D texture, Vector2 position, ContentManager Content)
{
//Getting the initialized stuff
button1 = Content.Load<Texture2D>("button_temp");
button1Pos.X = 30;
button1Pos.Y = 30;
}
public void Draw(SpriteBatch spriteBatch)
{
graphics.GraphicsDevice.Clear(Color.Black);
spriteBatch = new SpriteBatch(graphicsDevice);
//Just drawing the Sprite
spriteBatch.Begin();
spriteBatch.Draw(button1, new Rectangle(30, 30, 214, 101), Color.White);
spriteBatch.End();
}
}
}
Hope you can find an answer.
I can see many mistakes in your code, I would left a comment pointing all of the, but its too long.
Most of the mistakes come from this: you're not inheriting from Game class. Your line class Main_Menu should be class Main_Menu : Game. Always use this template for a game class:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MyGame
{
public class MyGame : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
From here on, you must fill this template with the following in mind:
Create your memory-only objects in the Initialize method;
Load and create file-related objects in the LoadContent method;
Add your game logic in the Update method;
Add your drawing logic in the Draw method;
Usually, do not bother with the constructor or the UnloadContent method.
Connecting your existing code with the template, we get the following:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Test_Game
{
public class Main_Menu : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 buttonPos; // our button position
Texture2D button; // our button texture
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
buttonPos = new Vector2(30, 30); // X=30, Y=30
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
button = Content.Load<Texture2D>("button_temp"); // load texture
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
// here we would add game logic
// things like moving game objects, detecting collisions, etc
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
// draw our button
int buttonWidth = 214;
int buttonHeight = 101;
spriteBatch.Draw(button, new Rectangle(buttonPos.X, buttonPos.Y, buttonWidth, buttonHeight), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
This template is used in every game for the MonoGame and XNA frameworks, so you should find a LOT of content on the web about what each method of the Game class does.

C# XNA - randomly draw a texture on the top X axis

So I'm making a space invaders game. I want to have meteors spawning from different/random locations on the entire 0 coordinate of X. How should I ahieve this? I've seen people using Lists and Random()s but I need a code for my meteorGenerator class. Then I'll call it's methods in Game1
When the meteors are drawn they should fall down at the bottom of the screen and disappear.
I saw an answer here on SO and implemented it to my class:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
namespace SpaceInvaders
{
class meteorGenerator
{
public static Vector2 m_Pos;
public Vector2 m_Position
{
get { return m_Pos; }
set { m_Pos = value; }
}
Texture2D m_Texture { get; set; }
Texture2D m_MeteorsTex;
public meteorGenerator(Texture2D m_Tex, Vector2 m_Pos)
{
m_Position = m_Pos;
m_Texture = m_Tex;
}
List<meteorGenerator> m_MeteorsList = new List<meteorGenerator>();
static readonly Random rnd = new Random();
public void LoadContent(ContentManager Content)
{
m_MeteorsTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\thePlan\\meteorSpawn");
}
public void Update(GameTime gameTime)
{
if (m_MeteorsList.Count() < 4)
{
m_MeteorsList.Add(new meteorGenerator(m_MeteorsTex, new Vector2(rnd.Next(30, 610), rnd.Next(30, 450))));
}
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (meteorGenerator m_Meteor in m_MeteorsList)
{
spriteBatch.Draw(m_Meteor.m_Texture, m_Meteor.m_Position, Color.White);
}
}
}
}
But when I try to instance the constructor of the class in Game1 I get an error:
meteorGenerator m_MeteorGenerator;
protected override void Initialize()
{
// TODO: Add your initialization logic here.
m_MeteorGenerator = new meteorGenerator(meteorGenerator.m_Tex, meteorGenerator.m_Pos);
}
Error 1 'SpaceInvaders.meteorGenerator' does not contain a definition for 'm_Tex'
I think this will do the trick.
I renamed the parameters for the constructor to vTexture and vPos but I agree with the comments this is old style coding and very confusing. Now we would use
public Vector2 position
public Vector2 Position
{
get { return position }
set { position = value; }
}
public meteorGenerator(Texture2D texture, Vector2 position)
{
Position = texture;
Texture = position;
}
But that is splitting hairs for now.
What I did change is m_MeteorsList, LoadContent, Update and Draw are now static.
you can just call
meteorGenerator.Loadcontent(Content) // to load your content
meteorGenerator.Update(gameTime) // will generate the meteors
meteorGenerator.Draw(spriteBatch) // will draw them.
No need to have multiple instances of the meteorGenerator class.
I honestly don't think this good practice as I would have used a separate Meteor class or struct to store information about meteors.
namespace SpaceInvaders
{
class meteorGenerator
{
public Vector2 m_Pos;
public Vector2 m_Position
{
get { return m_Pos; }
set { m_Pos = value; }
}
Texture2D m_Texture { get; set; }
Texture2D m_MeteorsTex;
public meteorGenerator(Texture2D vTex, Vector2 vPos)
{
m_Position = vPos;
m_Texture = vTex;
}
static List<meteorGenerator> m_MeteorsList = new List<meteorGenerator>();
static readonly Random rnd = new Random();
public static void LoadContent(ContentManager Content)
{
m_MeteorsTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\thePlan\\meteorSpawn");
}
public static void Update(GameTime gameTime)
{
if (m_MeteorsList.Count() < 4)
{
m_MeteorsList.Add(new meteorGenerator(m_MeteorsTex, new Vector2(rnd.Next(30, 610), rnd.Next(30, 450))));
}
}
public static void Draw(SpriteBatch spriteBatch)
{
foreach (meteorGenerator m_Meteor in m_MeteorsList)
{
spriteBatch.Draw(m_Meteor.m_Texture, m_Meteor.m_Position, Color.White);
}
}
}
}

Xna Movement not working

Hey since 1 week I am coding C# Xna and I ran into a problem
I hope you can help me.
The problem is that I made a Main class where I draw everything and I have a class for Control's
But now the control's wont work.
Main Class:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Pong_0._0._0._1
{
public class Main : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Texture2D paddel1;
public Texture2D paddel2;
public Texture2D border1;
public Texture2D border2;
public Vector2 paddel1Pos;
public Vector2 paddel2Pos;
public Vector2 border1Pos;
public Vector2 border2Pos;
public static int ScreenWidth = 1024;
public static int ScreenHeight = 768;
Paddels pads;
public Main()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = ScreenWidth;
graphics.PreferredBackBufferHeight = ScreenHeight;
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
pads = new Paddels();
pads.Initialize();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
paddel1 = Content.Load<Texture2D>("BOP");
paddel2 = Content.Load<Texture2D>("BOP");
border1 = Content.Load<Texture2D>("BOB");
border2 = Content.Load<Texture2D>("BOB");
paddel1Pos = new Vector2(ScreenWidth / 16, ScreenHeight / 2 - paddel1.Height / 2);
paddel2Pos = new Vector2((ScreenWidth - paddel2.Width) - ScreenWidth / 16 , ScreenHeight / 2 - paddel2.Height / 2);
border1Pos = new Vector2(ScreenWidth / 2 - (border1.Width / 2) , ScreenHeight / 12);
border2Pos = new Vector2(ScreenWidth / 2 - (border2.Width / 2), (ScreenHeight - border2.Height) - ScreenHeight / 12);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
pads.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Tomato);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(paddel1, paddel1Pos, Color.White);
spriteBatch.Draw(paddel2, paddel2Pos, Color.White);
spriteBatch.Draw(border1, border1Pos, Color.White);
spriteBatch.Draw(border2, border2Pos, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Control Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Pong_0._0._0._1
{
public class Paddels
{
public Main main;
public int SPEED = 2;
public Paddels()
{
}
public void Initialize()
{
main = new Main();
}
public void Update(GameTime gameTime)
{
KeyboardState KBS = Keyboard.GetState();
if (KBS.IsKeyDown(Keys.Up))
{
main.paddel1Pos.Y = main.paddel1Pos.Y + SPEED;
}
else if (KBS.IsKeyDown(Keys.Down))
{
}
if (KBS.IsKeyDown(Keys.W))
{
}
else if (KBS.IsKeyDown(Keys.S))
{
}
}
}
}
I could do it all in one class but I want to learn to use multiple classes and the never work.
Thanks and hope to hear from anyone soon.
You are creating a new "main" in your "Paddles" class. This new "Main" object has no knowledge of the actual one.
To solve this, just pass in the existing one when creating the paddles like this:
pads.Initialize(this);
With your initialize function changed to:
public void Initialize(Main parent)
{
main = parent;
}
In general though, you are breaking encapsulation by doing this. The "Paddles" class should hold all the textures, update logic, and draw logic for the paddle game objects. The main class shouldn't need any of that, since it is passing it off to the new class, and the "Paddles" class shouldn't need any knowledge of the "Main" class (at least in your example).
Please let me know if you would like any clarification or extra informatino.
You need to put your paddle position, and paddle texture inside of your paddle class.
Then you need to make the paddle draw itself using these values.

How do I use the variable in this accessor?

I am trying to use an accessor, as it seems to me that that is the only way to accomplish what I want to do. Here is my code:
Game1.cs
public class GroundTexture
{
private Texture2D dirt;
public Texture2D Dirt
{
get
{
return dirt;
}
set
{
dirt = value;
}
}
}
public class Main : Game
{
public static Texture2D texture = tile.Texture;
GroundTexture groundTexture = new GroundTexture();
public static Texture2D dirt;
protected override void LoadContent()
{
Tile tile = (Tile)currentLevel.GetTile(20, 20);
dirt = Content.Load<Texture2D>("Dirt");
groundTexture.Dirt = dirt;
Texture2D texture = tile.Texture;
}
protected override void Update(GameTime gameTime)
{
if (texture == groundTexture.Dirt)
{
player.TileCollision(groundBounds);
}
base.Update(gameTime);
}
}
I removed irrelevant information from the LoadContent and Update functions.
On the following line:
if (texture == groundTexture.Dirt)
I am getting the error
Operator '==' cannot be applied to operands of type 'Microsoft.Xna.Framework.Graphics.Texture2D' and 'Game1.GroundTexture'
Am I using the accessor correctly? And why do I get this error? "Dirt" is Texture2D, so they should be comparable.
This using a few functions from a program called Realm Factory, which is a tile editor. The numbers "20, 20" are just a sample of the level I made below:
http://i.stack.imgur.com/d8cO3.png
tile.Texture returns the sprite, which here is the content item Dirt.png
Thank you very much!

How do I fix this System.StackOverFlow exception in my Pong code?

I am trying to make Pong in XNA/C# using a class for the Paddle and Ball
Game1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Pong
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Paddle Paddle1 = new Paddle();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
Paddle1.Draw();
base.Draw(gameTime);
}
}
}
Paddle.cs:
namespace Pong
{
class Paddle
{
SpriteBatch spriteBatch;
ContentManager Content;
Texture2D paddle1;
Texture2D paddle2;
Vector2 Paddle1;
Vector2 Paddle2;
public void LoadContent()
{
paddle1 = Content.Load<Texture2D>("pongpaddle1");
Paddle1 = new Vector2();
Paddle1.X = 50;
Paddle1.Y = 50;
}
public void Draw()
{
spriteBatch.Begin(); //Causes NullReferenceException was unhandled, Object reference not set to an instance of an object.
spriteBatch.Draw(paddle1, Paddle1, Color.White);
spriteBatch.End();
}
}
}
I don't have anything in the Ball class yet, but it will use similar methods to Paddle.cs
Every time I've ran the code, I keep getting a System.StackOverFlow exception whenever it hits this line of code in Game1.cs:
Paddle Paddle1 = new Paddle();
How do I fix this? I don't see how it's run out of memory already.
EDIT: Updated code.
What's happening here is that Paddle inherits Game1. Game1 creates new Paddles:
Paddle Paddle1 = new Paddle();
Paddle Paddle2 = new Paddle();
Those Paddles are Games that need to initialize their own set of Paddles. Infinite recursion! I'm not sure how XNA works, but if that's how the inheritance should be, just move your initializations to Initialize():
// TODO: Add your initialization logic here
base.Initialize();
this.Paddle1 = new Paddle();
this.Paddle2 = new Paddle();
I kind of doubt that a game object should inherit from the game itself, though. That would seem like a rather poor design decision.
public class Game1 : Microsoft.Xna.Framework.Game
{
Paddle Paddle1 = new Paddle();
Paddle Paddle2 = new Paddle();
...
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Paddle1.LoadContent();
}
...
}
class Paddle : Game1
{
...
protected override void LoadContent()
{
Paddle1 = new Vector2();
Paddle1.X = 50;
Paddle1.Y = 50;
base.LoadContent();
}
...
}
Two big problems here, there is a recursive LoadContent call. Not to mention your paddles have paddles which have paddles... Why is your paddle inheriting from Game1? It almost definitely shouldn't be.
Also your paddle instances instantiate other paddle instances, so you're in a loop of instantiating other paddle classes.
It seems like you might want to take a step back and just get used to some basic code first? For what it's worth, I wrote pong in xna for fun a few years back, it's a bit messy, but it might give you some starting help.
Here is an example of a paddle class based off the DrawableGameComponent class (drawn in primatives so it's a bit verbose):
public class Paddle : DrawableGameComponent
{
private readonly VertexPositionColor[] _vertices = new VertexPositionColor[6];
private readonly float _width;
private readonly float _height;
private IndexBuffer _indexbuffer;
private VertexBuffer _vertexbuffer;
public Vector3 Position { get; set; }
public Vector3 Direction { get; set; }
public float Speed { get; set; }
public Paddle(Game game, float width, float height)
: base(game)
{
_width = width;
_height = height;
}
protected override void LoadContent()
{
base.LoadContent();
_vertices[0].Position = new Vector3(0, 0, 0);
_vertices[0].Color = Color.Red;
_vertices[1].Position = new Vector3(_width, _height, 0);
_vertices[1].Color = Color.Green;
_vertices[2].Position = new Vector3(0, _height, 0);
_vertices[2].Color = Color.Blue;
_vertices[3].Position = new Vector3(_width, 0, 0);
_vertices[3].Color = Color.Green;
_vertexbuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), _vertices.Length, BufferUsage.WriteOnly);
_vertexbuffer.SetData(_vertices);
var indices = new short[6];
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 0;
indices[4] = 3;
indices[5] = 1;
_indexbuffer = new IndexBuffer(GraphicsDevice, typeof(short), 6, BufferUsage.WriteOnly);
_indexbuffer.SetData(indices);
}
public BoundingBox GetBoundingBox()
{
return new BoundingBox(Position, Position + new Vector3(_width, _height, 0));
}
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
GraphicsDevice.SetVertexBuffer(_vertexbuffer);
GraphicsDevice.Indices = _indexbuffer;
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
}
}

Categories

Resources