In the game logic part of my game, where you check for input, why is the state my object had before is not being used in further evaluations of my function?
My game class:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.Diagnostics;
namespace MonoRPG
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
: base()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
Player playerObject = new Player(this, this.Content, this.spriteBatch);
KeyboardState oldState;
KeyboardState newState = Keyboard.GetState();
if (newState.IsKeyDown(Keys.Right))
{
Debug.WriteLine("right key pressed");
}
oldState = newState;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
renderMap createMap = new renderMap(this, this.Content, this.spriteBatch);
Player playerObject = new Player(this, this.Content, this.spriteBatch);
createMap.RenderMap();
playerObject.drawPlayer();
playerObject.positionX = playerObject.positionX + 10;
base.Draw(gameTime);
}
}
}
And my player class:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.Diagnostics;
namespace MonoRPG
{
public class Player
{
public int positionX = 0;
public int positionY = 0;
Game1 draw = new Game1();
ContentManager gameContent;
SpriteBatch playerSprites;
KeyboardState oldState;
public Player(Game1 canvas, ContentManager content, SpriteBatch spriteBatch)
{
draw = canvas;
gameContent = content;
playerSprites = spriteBatch;
}
public Player()
{
}
public void drawPlayer()
{
Texture2D playerTexture = gameContent.Load<Texture2D>("player/player.png");
playerSprites.Begin();
playerSprites.Draw(playerTexture, new Vector2(positionX, positionY), Color.Red);
playerSprites.End();
}
public void playerMove(KeyboardState keyState, KeyboardState oldState)
{
positionX = positionX + 1;
}
}
}
The drawing is working fine, but the position of the rectangle sprite I'm using for the player won't change. Is the problem related to how I'm creating the objects? I tried declaring outside the functions, but then I can't use the this keyword. How can I call the functions on my existing objects?
You should initialize (create) objects in the Initialize or LoadContent methods. The object should be created only once, not every update of the game. The Initialize and LoadContent happens only once, at the startup of the game (the Initialize first, the LoadContent when you call base.Initialize()).
When you put code in the Update method, it is executed at every update (usually, every frame) of the game. Your code should look more like this
Player player;
KeyboardState oldState; // player and oldState belongs to the whole game
protected override void Initialize() {
// remember that spriteBatch is still null here
player = new Player(this, Content, spriteBatch);
// the line below initializes the spriteBatch
// check the comments to see why this exact code won't work
base.Initialize();
}
protected override void Update(GameTime gameTime) {
KeyboardState newState = Keyboard.GetState(); // newState belongs to this exact update only
if (newState.IsKeyDown(Keys.Right) && oldState.IsKeyUp(Keys.Right)) {
player.playerMove(newState, oldState); // not sure why you wish to pass these arguments?
}
oldState = newState;
}
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.CornflowerBlue);
renderMap createMap = new renderMap(this, this.Content, this.spriteBatch);
createMap.RenderMap();
// the player here is the same player in the Update method
player.drawPlayer();
base.Draw(gameTime);
}
Related
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.
I'm trying to animate a character and make him walk left and right. While I've learned how to make a basic animation, I can't figure out how to switch between them.
When the character walks left I'd like him to switch from the 'idle' (animatedSprite) animation to the 'walking left'(animatedSprite2) animation.
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 WalkingAnimation
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private AnimatedSprite animatedSprite, animatedSprite2;
private Vector2 position = new Vector2(350f, 200f);
private KeyboardState keyState;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Texture2D texture = Content.Load<Texture2D>("Idle");
Texture2D texture2 = Content.Load<Texture2D>("Run");
animatedSprite = new AnimatedSprite(texture, 1, 11);
animatedSprite2 = new AnimatedSprite(texture2, 1, 10);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Q))
{
position.X -= 3;
}
if (keyState.IsKeyDown(Keys.P))
{
position.X += 3;
}
base.Update(gameTime);
animatedSprite.Update();
animatedSprite2.Update();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
animatedSprite.Draw(spriteBatch, position);
}
}
}
Like you so clearly expressed in your question, all you need to do is store a Boolean which describes whether the character is idle or not:
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 WalkingAnimation
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private AnimatedSprite animatedSprite, animatedSprite2;
private Vector2 position = new Vector2(350f, 200f);
private KeyboardState keyState;
private bool idle;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
// Start of as idle
idle = true;
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// Textures
Texture2D texture = Content.Load<Texture2D>("Idle");
Texture2D texture2 = Content.Load<Texture2D>("Run");
// Animations
animatedSprite = new AnimatedSprite(texture, 1, 11);
animatedSprite2 = new AnimatedSprite(texture2, 1, 10);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
keyState = Keyboard.GetState();
idle = true; // If the character doesn't move this will stay true
if (keyState.IsKeyDown(Keys.Q))
{
position.X -= 3;
idle = false;
}
if (keyState.IsKeyDown(Keys.P))
{
position.X += 3;
idle = false;
}
base.Update(gameTime);
animatedSprite.Update();
animatedSprite2.Update();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
if(idle)
animatedSprite.Draw(spriteBatch, position);
else
animatedSprite2.Draw(spriteBatch, position);
}
}
}
And that should do what you want.
Also, if animations get a bit clunky I would recommend using sprite sheets and creating your own Animation class which can store all the player variables. This is extra-useful if you decide to make your game multiplayer.
Lets say left and right walking anmation uses 5 frame. And it's on single texture.
Function Update
if State = Left
currentFrame +=1;
if currentFrame > 5 then currentFrame = 0
texureSource = new rectangle(32*currentFrame,0,32,23);
end if
if State = Right
currentFrame +=1;
if currentFrame > 5 then currentFrame = 0
texureSource = new rectangle(32*currentFrame,32,32,23);
end if
End Function
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.
I'm trying to get my cursor visible in an XNA game using isMouseVisible, but it doesn't want to work. The error:
'StopWatch.Game1' does not contain a definition for 'isMouseVisible' and no extension method 'isMouseVisible' accepting a first argument of type 'StopWatch.Game1' could be found
Code:
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 StopWatch
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
public SpriteBatch spriteBatch;
public static Game1 Instance;
StopWatch stopWatch;
public Game1()
{
this.isMouseVisible = true;
graphics = new GraphicsDeviceManager(this);
Instance = this;
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 512;
graphics.PreferredBackBufferHeight = 512;
}
protected override void Initialize()
{
stopWatch = new StopWatch();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
stopWatch.LoadContent();
}
protected override void UnloadContent()
{
stopWatch.UnloadContent();
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
stopWatch.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
stopWatch.Draw(gameTime);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Use IsMouseVisible = true
Capitalize the I
Remember, Using intelisence you can view methods and variables just by typing the beginning.
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);
}
}