Reset Texture2Ds in XNA - c#

I'm Having a problem with an experiment of mine. I'm trying to make a method that fuses one Texture2D into another Texture2D, then reloads the Texture2D, this time moving the pasted texture in a new spot. I plan to perfect upon this method in later projects where I paste in a spotlight onto a dark background to "illuminate" the scenery behind it. While I managed to make the paste-to method work perfectly, I am having some major problems with the second part of my plan. I have the pasted texture set to a Vector2, where the X member increases by 1 every frame, thus dragging the pasted texture along the background. However, the background doesn't reset after each frame, leaving a drag mark on the background where the previous image was. For the last half hour I've been working to cure this problem by resetting the background texture to make it clean again for when it has the other image pasted onto it.
Here is my code.
namespace CopyTest
{
public class Image
{
public Texture2D Bitmap;
public Vector2 Position;
public String BitmapName;
public Rectangle Viewport;
public Color Tint = Color.White;
public void ResetViewportDimensions()
{
this.Viewport = new Rectangle(this.Viewport.X, this.Viewport.Y, Bitmap.Width, Bitmap.Height);
}
public void Draw(SpriteBatch Target)
{
}
public void FinalizeBitmap(ContentManager Target)
{
this.Bitmap = null;
this.Bitmap = Target.Load<Texture2D>(this.BitmapName);
}
public Image(Vector2 Position, String BitmapName)
{
this.BitmapName = BitmapName;
this.Position = Position;
}
}
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Image Rainbow = new Image(new Vector2(0, 0), "Rainbow");
public Image Eye = new Image(new Vector2(0, 0), "Glow");
public Image PrintOut = new Image(new Vector2(0, 0), "Rainbow");
public Vector2 Destination = new Vector2(100, 50);
public void PrintTo(Image Source, Image Target, Vector2 Location)
{
Color[] A = new Color[Source.Bitmap.Width * Source.Bitmap.Width];
Color[] B = new Color[Target.Bitmap.Width * Target.Bitmap.Width];
int Y = 0;
int X = 0;
Source.Bitmap.GetData(A);
Target.Bitmap.GetData(B);
for (int i = 0; i < A.Length; i++)
{
B[(int)Location.X + X + (((int)Location.Y + Y) * Target.Bitmap.Width)] = A[i];
X++;
if (X == Source.Bitmap.Width)
{
X = 0;
Y++;
}
}
Target.Bitmap.SetData<Color>(B);
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Eye.FinalizeBitmap(Content);
Rainbow.FinalizeBitmap(Content);
PrintOut.FinalizeBitmap(Content);
Eye.ResetViewportDimensions();
Rainbow.ResetViewportDimensions();
PrintOut.ResetViewportDimensions();
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
GraphicsDevice.Textures[0] = null;
PrintOut.FinalizeBitmap(Content);
PrintTo(Eye, PrintOut, Destination);
Destination.Y++;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(PrintOut.Bitmap, PrintOut.Position, PrintOut.Viewport, PrintOut.Tint);
spriteBatch.End();
base.Draw(gameTime);
}
}
Any help at all would be greatly appreciated.

Related

Move enemy sprite depending on which side it spawns. c# monogame

So I have a code where the sprite spawn outside the window and I want it to move on a specific straight line depending on where it spawn say if the sprite spawn at the top it goes at the bottom and vise versa if the sprite then spawn to the left it will go right and vise versa.
Game1.cs code:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player player;
List<Enemy> enemies = new List<Enemy>();
public Vector2 ecords
{
get { return ecords; }
set { ecords = value; }
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
//player
Texture2D playertexture = Content.Load<Texture2D>("Player");
player = new Player(new Vector2(350, 175), playertexture);
//enemy
Texture2D enemytexture = Content.Load<Texture2D>("Enemy");
Random random = new Random();
var width = GraphicsDevice.Viewport.Width;
var height = GraphicsDevice.Viewport.Height;
for (int i = 0; i < 20; i++)
{
enemies.Add(new Enemy(new Vector2(random.Next(width, width + 100), random.Next(0, height)), enemytexture));
enemies.Add(new Enemy(new Vector2(random.Next(0 - 100, 0), random.Next(0, height)), enemytexture));
enemies.Add(new Enemy(new Vector2(random.Next(0, width), random.Next(-100, 0)), enemytexture));
enemies.Add(new Enemy(new Vector2(random.Next(0, width), random.Next(height, height + 100)), enemytexture));
}
}
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();
foreach (Enemy enemy in enemies)
{
enemy.Update(gameTime);
}
player.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (Enemy enemy in enemies)
{
enemy.Draw(spriteBatch);
}
player.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
Enemy.cs
class Enemy
{
Texture2D enemytexture;
Vector2 enemyposition;
public Enemy(Vector2 enemyposition, Texture2D enemytexture)
{
this.enemyposition = enemyposition;
this.enemytexture = enemytexture;
}
public void Update(GameTime gameTime)
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(enemytexture, enemyposition, Color.White);
}
}
One option would be to add a velocity (or you can name it movement/direction) to Enemy, passed to the constructor in a parameter just like your existing enemyposition. Then update the current position by adding the velocity in the Update method of Enemy:
public void Update(GameTime gameTime)
{
this.enemyposition += this.velocity;
}
You can use unit vectors (optionally multiplied by speed) for the velocity value:
var (left, right) = (-Vector2.UnitX, Vector2.UnitX);
var (up, down) = (-Vector2.UnitY, Vector2.UnitY);
float enemySpeed = 5.0f;
enemies.Add(new Enemy(
new Vector2(random.Next(width, width + 100), random.Next(0, height)),
enemytexture, velocity: left * enemySpeed));
// etc.

Monogame: Creating Draw Function In Class

I am wanting to make a tile class that contains it's own drawing function. Below is what I have so far. The error that is showing when I run the coder is. "System.InvalidOperationException: 'Begin cannot be called again until End has been successfully called." This error message shows up on the spirteBatch.Begin() inside the class.
namespace TileGame.v3
{
class Tile
{
public Texture2D texture { get; set; }
public int X { get; set; }
public int Y { get; set; }
public int sourceX { get; set; }
public int sourceY { get; set; }
public int width { get; set; }
public int height { get; set; }
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Begin();
spriteBatch.Draw(texture, new Vector2(X, Y), new Rectangle(sourceX, sourceY, width, height), Color.White);
spriteBatch.End();
Draw(gameTime, spriteBatch);
}
}
}
namespace TileGame.v3
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D mark;
Texture2D peep1;
Texture2D ocean;
int x1 = 100;
int y1 = 100;
int x2 = 400;
int y2 = 400;
int tempx;
int tempy;
bool win = false;
Tile a = new Tile();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
mark = Content.Load<Texture2D>("mark");
peep1 = Content.Load<Texture2D>("peep1");
ocean = Content.Load<Texture2D>("ocean");
a.texture = ocean;
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
tempx = x1;
x1 = x2;
x2 = tempx;
tempy = y1;
y1 = y2;
y2 = tempy;
win = true;
}
a.X = 100;
a.Y = 100;
a.sourceX = 300;
a.sourceY = 300;
a.width = 100;
a.height = 100;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
a.Draw(gameTime, spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
The texture is 1920x1080 and I have tested drawing and cutting it and it has worked fine.
First of all, your code is probably incomplete, but you are never calling the draw method anywhere.
Let say you are calling it, are you sure your texture is bigger than 300x300? And what does that texture contains for (x,y = 300,300 to x,y = 400,400)? Does it have any color data in those positions?
If you answer yes to all of this, we will need more code to be able to help you with that.
Side node: Since this is a tile, you probably are going to render many of them. You don't need to call .Begin() and .End() for all of them. Simply call Begin() before the first tile and End() after the last one and you're good to go. This will make rendering much faster.

How to move my sprite in MonoGame using C#

First off, i'm fairly new to programming, about 2 months in, i dabbled a bit in Console Application, WinForms, and still using the ugly console to get better at algorithms in general. But now, I wanna start digging into game programming, because that's the reason i wanted to learn programming. I stumbled upon MonoGame, and while it's harder than say Unity, I got an immense sense of achievement after creating something by just using code. I already made a Space Invaders and Pong but nothing related to sprite animation, using spritesheets and moving a player. So 2 days ago, I started a platformer, divided my spritesheet, got some animation down, and now that it's time to move my player, I'm completely lost. I tried reading some tutorials on vectors, but it doesn't help in my case. Maybe you can shed some light on the matter.
So, without further ado, here's the code:
Game.cs
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace MafiaJohnny
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private JohnnyPlayer johnnyPlayer;
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>("JohnnyDone");
johnnyPlayer = new JohnnyPlayer(texture, 2, 4);
}
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();
johnnyPlayer.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
johnnyPlayer.Draw(spriteBatch, new Vector2(200, 200));
base.Draw(gameTime);
}
}
}
JohnnyPlayer.cs
using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
namespace MafiaJohnny
{
class JohnnyPlayer
{
public Texture2D Texture { get; set; }
public int Rows { get; set; }
public int Columns { get; set; }
private int currentFrame;
private int totalFrames;
//Slow down frame animation
private int timeSinceLastFrame = 0;
private int millisecondsPerFrame = 400;
public JohnnyPlayer(Texture2D texture, int rows, int columns)
{
Texture = texture;
Rows = rows;
Columns = columns;
currentFrame = 0;
totalFrames = Rows * Columns;
}
public void Update (GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
KeyboardState keystate = Keyboard.GetState();
//Idle animation
if (keystate.GetPressedKeys().Length == 0)
currentFrame++;
timeSinceLastFrame = 0;
if (currentFrame == 2)
currentFrame = 0;
//Walking Animation
if (keystate.IsKeyDown(Keys.Left))
{
}
}
}
public void Draw (SpriteBatch spriteBatch, Vector2 location)
{
int width = Texture.Width/Columns;
int height = Texture.Height / Rows;
int row = (int) ((float) currentFrame/Columns);
int column = currentFrame % Columns;
Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);
spriteBatch.Begin();
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
spriteBatch.End();
}
}
}
So here's my code, find me an answer minions! Thank you is what I mean :)
You just need to change "location" so the sprite moves left/right/up/down. Also I recommend moving this code from JohnnyPlayer to another "controller" class.
Here:
http://www.gamefromscratch.com/post/2015/06/15/MonoGame-Tutorial-Creating-an-Application.aspx
They make a sprite and move it from left to right. In your case the texture on sprite changes in time (animation) but the movement is still the same.

XNA - No loading screen or menu

The problem is that it just shows a black screen and the loading mouse. It doesn't show the loading screen or the menu.
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteBatch mBatch;
Texture2D mTheQuantumBros2;
MenuComponent menuComponent;
public static Rectangle screen;
public static string GameState = "Menu";
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferMultiSampling = false;
graphics.IsFullScreen = true;
graphics.PreferredBackBufferWidth = 1366;
graphics.PreferredBackBufferHeight = 768;
graphics.ApplyChanges();
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
mBatch = new SpriteBatch(this.graphics.GraphicsDevice);
ContentManager aLoader = new ContentManager(this.Services);
aLoader.RootDirectory = "Content";
mTheQuantumBros2 = aLoader.Load<Texture2D>("TheQuantumBros2") as Texture2D;
menuComponent.LoadContent(Content);
}
protected override void UnloadContent()
{
}
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
switch (GameState)
{
case "Menu":
menuComponent.Update(gameTime);
break;
}
base.Update(gameTime);
}
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
switch (GameState)
{
case "Menu":
menuComponent.Draw(spriteBatch);
break;
}
base.Draw(gameTime);
}
}
Other class:
class MenuComponent
{
KeyboardState keyboard;
KeyboardState prevKeyboard;
SpriteBatch mBatch;
Texture2D mTheQuantumBros2;
GameTime gameTime;
MouseState mouse;
MouseState prevMouse;
SpriteFont spriteFont;
List<string> buttonList = new List<string>();
int selected = 0;
public MenuComponent()
{
buttonList.Add("Campaign");
buttonList.Add("Multiplayer");
buttonList.Add("Zombies");
buttonList.Add("Quit Game");
}
public void LoadContent(ContentManager Content)
{
spriteFont = Content.Load<SpriteFont>("Font");
}
public void Update(GameTime gameTime)
{
keyboard = Keyboard.GetState();
mouse = Mouse.GetState();
if (CheckKeyboard(Keys.Up))
{
if (selected > 0) selected--;
}
if (CheckKeyboard(Keys.Down))
{
if (selected < buttonList.Count - 1) selected++;
}
prevMouse = mouse;
prevKeyboard = keyboard;
}
public bool CheckMouse()
{
return (mouse.LeftButton == ButtonState.Pressed && prevMouse.LeftButton == ButtonState.Released);
}
public bool CheckKeyboard(Keys key)
{
return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyDown(key));
}
public void Draw(SpriteBatch spriteBatch)
{
Color color;
int linePadding = 3;
spriteBatch.Begin();
mBatch.Begin();
mBatch.Draw(mTheQuantumBros2, new Rectangle(300, 150, mTheQuantumBros2.Width, mTheQuantumBros2.Height), Color.White);
if (gameTime.TotalGameTime.TotalSeconds <= 3)
{
mBatch.End();
for (int i = 0; i < buttonList.Count; i++)
{
color = (i == selected) ? Color.LawnGreen : Color.Gold;
spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((Game1.screen.Width / 2) - (spriteFont.MeasureString(buttonList[i]).X / 2), (Game1.screen.Height / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
}
spriteBatch.End();
}
}
}
What is the purpose of this statement: (gameTime.TotalGameTime.TotalSeconds <= 3)?
Also, you are never updating the game time in the menu component. So, the aforementioned code would behave unexpectedly.
You are also calling spriteBatch.Begin() twice before end is called... So that should be giving you an error. Check those for issues.
EDIT: Since you are only calling spriteBatch.End() IF less than 3 seconds have passed, it will never be called again after that? I think.
As a potential fix... I would try only calling spriteBatch.Begin() and spriteBatch.End() inside of game1.cs. So:
//In your menu class
public void Draw(SpriteBatch spriteBatch)
{
Color color;
int linePadding = 3;
if (gameTime.TotalGameTime.TotalSeconds <= 3)
{
mBatch.Begin();
mBatch.Draw(mTheQuantumBros2, new Rectangle(300, 150, mTheQuantumBros2.Width, mTheQuantumBros2.Height), Color.White);
mBatch.End();
}
else
{
for (int i = 0; i < buttonList.Count; i++)
{
color = (i == selected) ? Color.LawnGreen : Color.Gold;
spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((Game1.screen.Width / 2) - (spriteFont.MeasureString(buttonList[i]).X / 2), (Game1.screen.Height / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
}
}
}
And back in game1.cs, call spriteBatch.End() right before base.Draw() is called.
In general, it is best to only use one spriteBatch, I believe it is simply faster than beginning and ending two different batches.
EDIT 2:
Just uhm... copy and paste I guess. It works fine after the adjustments I made (you can read about them at the bottom).
You're refactored code:
game1.cs:
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
MenuComponent menuComponent;
public static Rectangle screen;
public static string GameState = "Menu";
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferMultiSampling = false;
//graphics.IsFullScreen = true;
graphics.PreferredBackBufferWidth = 1366;
graphics.PreferredBackBufferHeight = 768;
graphics.ApplyChanges();
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
menuComponent = new MenuComponent();
menuComponent.LoadContent(Content, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
}
protected override void UnloadContent()
{
}
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
switch (GameState)
{
case "Menu":
menuComponent.Update(gameTime);
break;
}
base.Update(gameTime);
}
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
switch (GameState)
{
case "Menu":
menuComponent.Draw(spriteBatch, gameTime);
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
MenuComponent:
KeyboardState keyboard;
KeyboardState prevKeyboard;
MouseState mouse;
MouseState prevMouse;
GraphicsDevice graphicsDevice;
Texture2D mTheQuantumBros2;
SpriteFont spriteFont;
List<string> buttonList = new List<string>();
int selected = 0;
int screenWidth;
int screenHeight;
public MenuComponent()
{
buttonList.Add("Campaign");
buttonList.Add("Multiplayer");
buttonList.Add("Zombies");
buttonList.Add("Quit Game");
}
public void LoadContent(ContentManager Content, int _screenWidth, int _screenHeight)
{
spriteFont = Content.Load<SpriteFont>("Font");
mTheQuantumBros2 = Content.Load<Texture2D>("TheQuantumBros2");
screenHeight = _screenHeight;
screenWidth = _screenWidth;
}
public void Update(GameTime gameTime)
{
keyboard = Keyboard.GetState();
mouse = Mouse.GetState();
if (CheckKeyboard(Keys.Up))
{
if (selected > 0) selected--;
}
if (CheckKeyboard(Keys.Down))
{
if (selected < buttonList.Count - 1) selected++;
}
prevMouse = mouse;
prevKeyboard = keyboard;
}
public bool CheckMouse()
{
return (mouse.LeftButton == ButtonState.Pressed && prevMouse.LeftButton == ButtonState.Released);
}
public bool CheckKeyboard(Keys key)
{
return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyDown(key));
}
public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
{
Color color;
int linePadding = 3;
if (gameTime.TotalGameTime.TotalSeconds <= 3)
{
spriteBatch.Draw(mTheQuantumBros2, new Rectangle(300, 150, mTheQuantumBros2.Width, mTheQuantumBros2.Height), Color.White);
}
else
{
for (int i = 0; i < buttonList.Count; i++)
{
color = (i == selected) ? Color.LawnGreen : Color.Gold;
spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((screenWidth / 2) - (spriteFont.MeasureString(buttonList[i]).X / 2), (screenHeight / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
}
}
}
There were a bunch of little issues. For example, you were setting the menuComp = to a new menu component on every update call, so all the old variables in there would be lost, hence there was no images loading. SpriteBatch.Begin() was being called twice in a row, and then end was being called twice in a row. One spriteBatch (from game1.cs preferrably) should be used to draw. By sending that spritebatch through the method, you do not need to call begin again, and it is simply better not to for performance reasons. Last thing I noticed (that I can think of) was that the graphics device was not being set to anything because you were constantly creating a new version of it. Oh also, you were calling graphicsDevice.Clear() way too early, this only should be called once per draw (at the beginning, so old graphical info is removed from the screen).
I debugged your program/code and there are a few problems:
There is a declaration of MenuComponent menuComponent, but you didn't create an instance in the Game1() constructor. So add: menuComponent = new MenuComponent()
There is a declaration of SpriteBatch mBatch, but you didn't create an instance in the MenuComponent() constructor. So create one. The same for GameTime gameTime variable.
At the moment I don't have SpriteFont and TheQuantumBros2 texture. Is it possible that you upload your project and share it?

'particleEngine' being drawn against a vector

I'm using XNA and C#.
I have a problem with calling a vector variable from my particleEmitter. I can draw the particle just fine if it is static or not moving. And when I have a vector variable that is set to a fixed position of (x,y) it's okay and draws on the screen. But if I have a vector variable that has been set to move in the x or y axis it does not draw at all.
Declared variables:
Vector2 shipPos;
float shipMovement;
ParticleEngine particleEngine;
And a method that loads stuff about what should happen with the vectors and the way it should behave:
public void loadEmitter(GameTime gameTime)
{
shipMovement = 2f;
shipPos.Y -= shipMovement;
particleEngine.EmitterLocation = new Vector2(shipPos.X,shipPos.Y);
}
I'm trying to get particleEngine to trail the movement of a ship. What I can't seem to do is get it to draw when I set this up to happen.
Other info: ParticleEngine is a class in itself and basically sets some parameters about how the particles I will be drawing should behave. I have other screens with the spritebatch Begin and End calls. Other than that, here's the code for my main class:
namespace PlanetDrill2
{
class LaunchScreen : Screen
{
Texture2D LaunchScreenTexture;
Texture2D shipLaunch;
Vector2 shipPos;
float shipMovement;
ParticleEngine particleEngine;
Vector2 smokePos;
public LaunchScreen(Game game, SpriteBatch batch, ChangeScreen changeScreen)
: base(game, batch, changeScreen)
{
}
protected override void SetupInputs()
{
base.SetupInputs();
}
public override void Activate()
{
base.Activate();
}
public void LaunchShip()
{
}
public void loadEmitter(GameTime gameTime)
{
shipMovement = 2f;
shipPos.Y -= shipMovement;
particleEngine.EmitterLocation = new Vector2(shipPos.X,shipPos.Y);
}
protected override void LoadScreenContent(ContentManager content)
{
LaunchScreenTexture = content.Load<Texture2D>("launchTest");
shipLaunch = content.Load<Texture2D>("shipLaunch");
List<Texture2D> textures = new List<Texture2D>();
textures.Add(content.Load<Texture2D>("smoketexture"));
particleEngine = new ParticleEngine(textures, new Vector2(0, 0));
base.LoadScreenContent(content);
}
protected override void UpdateScreen(GameTime gameTime, DisplayOrientation screenOrientation)
{
//if (gameTime.TotalGameTime.Seconds>10)
//{
// changeScreenDelegate(ScreenState.UMA);
//}
loadEmitter(gameTime);
particleEngine.Update();
base.UpdateScreen(gameTime, screenOrientation);
}
protected override void DrawScreen(SpriteBatch batch, DisplayOrientation screenOrientation)
{
batch.Draw(LaunchScreenTexture, Vector2.Zero, Color.White);
batch.Draw(shipLaunch, new Vector2(80, 450) +shipPos, Color.White);
particleEngine.Draw(batch);
base.DrawScreen(batch, screenOrientation);
}
protected override void SaveScreenState()
{
base.SaveScreenState();
}
} // end class LaunchScreen
} // end namespace PlanetDrill2
From here
batch.Draw(shipLaunch, new Vector2(80, 450) +shipPos, Color.White);
particleEngine.Draw(batch);
It looks like you are drawing the ship relative to [80, 450], but you are not applying this offset to the particleEngine.

Categories