Xna - Objects sharing the same texture - c#

In my game, I have an Ai class which is basically just a linked list of every ai in my game. this same class holds the default textures for every ai, and all of my ai's seperate classes inherit from this class, that way they all can inherit the default textures that were already loaded by the ai class. However, I seem to be having problems with this. My game never loads up the gui when ran, and through debugging, it seems like the game has problems with the textures I am passing. Are you not able to load a single texture and pass that same texture for another object to use?
AI class:
class AIs
{
private GraphicsDevice graphics;
private ContentManager content;
private SpriteBatch spriteBatch;
private LinkedList<object> ais;
private LinkNode<object> current;
//Default Textures
private Texture2D robotTexture
// Default Color Datas
private Color[] robotColorData;
public AIs()
{
}
public void Load(ContentManager content, GraphicsDevice graphics, SpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
this.graphics = graphics;
this.content = content;
// Loading Default Textures
robotTexture = content.Load<Texture2D>("robot");
// Loading Default Color Data
robotColorData = new Color[robotTexture.Width * robotTexture.Height];
robotTexture.GetData(robotColorData);
}
public void Update()
{
current = ais.getHead();
while (current.getNext() != null)
{
if (current.getData() is Robot)
{
((Robot)current.getData()).Update();
}
}
}
public void Draw()
{
current = ais.getHead();
while (current.getNext() != null)
{
if (current.getData() is Robot)
{
((Robot)current.getData()).Draw();
}
}
}
public addRobot(float spawnX, float spawnY)
{
Robot temp = new Robot(spawnX, spawnY);
temp.Load(content, graphics, spriteBatch);
ais.add(temp);
}
public Texture2D getRobotTexture()
{
return robotTexture;
}
public Color[] getRobotColorData()
{
return robotColorData;
}
}
Robot Class:
class Robot : AIs
{
private GraphicsDevice graphics;
private ContentManager content;
private SpriteBatch spriteBatch;
private Texture2D robotTexture;
private Color[] robotColorData;
private Rectangle robotRectangle;
private Vector2 robotPosition = Vector2.Zero;
public Robot(float spawnX, float spawnY)
{
robotPosition = new Vector2(spawnX, spawnY);
}
new public void Load(ContentManager content, GraphicsDevice graphics, SpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
this.graphics = graphics;
this.content = content;
robotTexture = getRobotTexture();
robotColorData = getRobotColorData();
}
new public void Update()
{
robotRectangle = new Rectangle((int)robotPosition.X, (int)robotPosition.Y, robotTexture.Width, robotTexture.Height);
}
new public void Draw()
{
spriteBatch.Draw(robotTexture, robotPosition, Color.White);
}
}

Turns out, all you have to do is add the keyword "static" next to the texture and color data.
If you don't put static, then when the class is created, it will inherit the methods and variables, but the variables will be new, null, because it is a new instance of the class. So putting static next to it makes the value stay the same for all instances.
In the AI class:
//Default Textures
private static Texture2D robotTexture
// Default Color Datas
private static Color[] robotColorData;

It seems the problem here is in your use of inheritance.
What happens is that you run your robots Load-method, which fetches robotTexture from itself through AIs getRobotTexture-method. That method just returns robotTexture, so you might as well write robotTexture = robotTexture.
But since this instance has not ran AIs.Load, robotTexture is null.
To be brutally honest; Read up on inheritance!
To be more helpful;
It seems that what you are really after is having a robotmanager that simplifies the spawning of robots. For this, inheritance is not the answer. Try something like this instead:
public class RobotManager
{
private SpriteBatch spriteBatch;
private Texture2D robotTexture;
private List<Robot> robots;
public RobotManager(SpriteBatch spriteBatch, Texture2D texture)
{
this.spriteBatch = spriteBatch;
this.robotTexture = texture;
robots = new List<Robot>();
}
public void Update()
{
foreach (var robot in robots)
robot.Update();
}
public void Draw()
{
foreach (var robot in robots)
robot.Draw();
}
public void AddRobot(Vector2 position, Texture2D customTexture = null)
{
//Creates a new robot with position set and custom texture if specified
var newRobot = new Robot(spriteBatch, position, (customTexture == null) ? robotTexture : customTexture);
robots.Add(newRobot);
}
}

Related

Monogame Sprite Class

Im working on a level editor in monogame for my engine.
I want to make a class where i can call a simple function and it will draw a sprite.
This is the function i want to call - and as you may know you have to be able to load and unload content and use the draw method.
Question: How would I make this be able to use those so that all I have to do is call this function and it works?
Here is the function:
public static void DrawSprite(Texture2D Texture, string Path, Vector2 Position, Color Color)
{
}
If you are going to leave the drawing to a single static method then you would be restricting what you are able to draw. I suggest creating an interface and do some abstraction.
Interface
public interface IGameObject
{
void Update(GameTime gameTime);
void Draw();
}
Utility Class
public sealed class GameUtility
{
private static GameUtility instance = null;
private static readonly object _lock = new object();
public ContentManager ContentManager { get; private set; }
public SpriteBatch SpriteBatch { get; private set; }
public static GameUtility Instance
{
get
{
lock (_lock)
{
if (instance == null)
{
instance = new GameUtility();
}
return instance;
}
}
}
public void SetContentManager(ContentManager contentManager)
{
this.ContentManager = contentManager;
}
public void SetSpriteBatch(SpriteBatch spriteBatch)
{
this.SpriteBatch = spriteBatch;
}
public GameUtility(ContentManager contentManager, SpriteBatch spriteBatch)
{
this.contentManager = contentManager;
this.spriteBatch = spriteBatch;
}
}
Game Objects
public class Hero : IGameObject
{
private Texture2D texture;
private Vector2 position;
private Color color;
public Hero(string path)
{
texture = GameUtility.Instance.ContentManager.Load<Texture2D>(path);
}
public void Update(GameTime gameTime)
{
// Do game update logic
}
public void Draw()
{
GameUtility.Instance.SpriteBatch.Begin();
GameUtility.Instance.SpriteBatch.Draw(texture, position, color);
GameUtility.Instance.SpriteBatch.End();
}
}
Game Class
Initialize the GameUtility
GameUtility.Instance.SetContentManager(contentManager);
GameUtility.Instance.SetSpriteBatch(spriteBatch);
Create the game objects
gameObects = new List<IGameObject>();
gameObjects.Add(new Hero("some path"));
Utilize the interface
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
foreach (IGameObject gameObject in gameObjects)
{
gameObject.Draw();
}
base.Draw(gameTime);
}
The beauty of this approach is you can perform different drawings based on your needs. For example, you could use a Rectangle instead of Vector2 based on different scenarios. You can also draw a sprite font or something else.
For unloading content, there is only one option which is
GameUtility.Instance.ContentManager.Unload();
You better unload content during your transition to the next level as calling ContentManager.Unload() will dispose all resources. As to why it disposes everything in one go, I don't really understand but that is the design.
Hope this answer give you some insight. I would not suggest creating this public static void DrawSprite.

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!

'GameStates': cannot reference a type through an expression; try 'menuinterface.Game1.GameStates' instead

I changed my code but it still doesn't work. I get this error message in the Intro class:
'GameStates': cannot reference a type through an expression; try 'menuinterface.Game1.GameStates' instead
What is wrong? I want to set the gamestate to MenuState if the player presses the Space Key.
Game1 class:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private IState currentState;
public enum GameStates
{
IntroState = 0,
MenuState = 1,
MaingameState = 2,
}
public GameStates CurrentState
{
get { return currentGameState; }
set { currentGameState = value; }
}
private GameStates currentGameState = GameStates.IntroState;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
currentState = new Intro(this);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
currentState.Load(Content);
}
protected override void Update(GameTime gameTime)
{
currentState.Update(gameTime);
KeyboardState kbState = Keyboard.GetState();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
currentState.Render(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
Interface:
public interface IState
{
void Load(ContentManager content);
void Update(GameTime gametime);
void Render(SpriteBatch batch);
}
Intro class:
public class Intro : IState
{
private IState currentState;
Texture2D Menuscreen;
private Game1 game1;
public Intro(Game1 game)
{
game1 = game;
}
public void Load(ContentManager content)
{
Menuscreen = content.Load<Texture2D>("menu");
}
public void Update(GameTime gametime)
{
KeyboardState kbState = Keyboard.GetState();
if (kbState.IsKeyDown(Keys.Space))
game1.CurrentState = game1.GameStates.IntroState;
currentState = new Menu(game1);
}
public void Render(SpriteBatch batch)
{
batch.Draw(Menuscreen, new Rectangle(0, 0, 1280, 720), Color.White);
}
}
Your problem is not understanding how enumerative types work. Specifically, this line:
game1.GameStates = IntroState;
First, let's discuss a little of what an enum actually is. It's a structure that simply assigns names to integer values, and can then refer to each value by name, and thus make code more readable (as it's far easier to understand direction = Dir.Up than direction = 1).
Notice how I use those two example statements, though. In the code, you treat the enum as a type not as a variable, and this is the problem you're encountering. In fact, your problem is twofold. The first issue id that you're trying to assign a value to a structure, which is similar to writing int = 4 - i.e. it doesn't make sense. Your second issue is that enums are not global, so IntroState has no meaning outside game1.
Interestingly enough, you've set up the system correctly, as you have the currentGameState variable, which is what you actually intend to change. However, it is a private variable, disallowing access to it from outside the game1 class. You can either make the variable public, or create a property to access it. The latter is good practice, as it allows you to control how the variable is set. To create this, you can use something like this:
public GameStates CurrentState
{
get { return currentGameState; }
set { currentGameState = value; }
}
Once you have this in your game1 class, you can set the game state like so:
game1.CurrentState = Game1.GameStates.IntroState;
Notice how this code tells the program where to look for what you want. IntroState is part of a structure (GameStates) within game1, and so needs to be accessed explicitly.
Hopefully, this has helped you to understand how enumerative types work, and why the code you wrote doesn't make sense, and thus why it breaks.
Do what the error message says:
game1.GameStates = Game1.GameStates.IntroState;
You need to fully qualify that enum value.

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);
}
}

How to make texture move

I have problem. I have tryed how to make my texture move, but my solution is slow and it is not working. Does anybody know how to make texture2D move using C# XNAGamestudio. ANybody please help me!
EDIT:
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Hashtable Objects = new Hashtable();
public Hashtable XPOS = new Hashtable();
public Hashtable YPOS = new Hashtable();
public int NUM = 0;
public bool UP = true;
public bool DOWN = false;
public bool LEFT = false;
public bool RIGHT = false;
......
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
this.AddObject("Nameee", "C:\\Box.png");
// TODO: Add your update logic here
if (UP)
{
if (NUM != 25)
{
AppendObject(new Vector2((float)this.XPOS["Nameee"], (float)this.XPOS["Nameee"] - NUM), "Nameee");
NUM++;
Thread.Sleep(100);
}
else
UP = false;
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
//GraphicsDevice.Clear(Color.CornflowerBlue);
//spriteBatch.Begin();
//spriteBatch.End();
//base.Draw(gameTime);
}
public void AddObject(string TagName, string ObjectImage)
{
Texture2D fileTexture;
using (FileStream fileStream = new FileStream(#ObjectImage, FileMode.Open))
{
fileTexture = Texture2D.FromStream(graphics.GraphicsDevice, fileStream);
}
if (!this.Objects.ContainsKey(TagName))
{
this.Objects.Add(TagName, fileTexture);
this.XPOS.Add(TagName, 0.0F);
this.YPOS.Add(TagName, 50.0F);
}
}
public void AppendObject(Vector2 pos, string tagName)
{
spriteBatch.Begin();
spriteBatch.Draw((Texture2D)this.Objects[tagName], new Vector2((float)this.XPOS[tagName], (float)this.YPOS[tagName]), Color.White);
spriteBatch.End();
}
Hmm, well technically, this looks like it should move a texture along the X axis.
As for performance, you may want to take these things into consideration:
You are calling this.AddObject("Nameee", "C:\\Box.png"); on every update. That's not a good thing. Call this once in your LoadContent() method.
Instead of using Thread.Sleep(100);, I would suggest tracking the time elapsed with GameTime. (Hit the comment if you need an example of how to do this)
You are creating a new vector2 on each pass for the texture position. While this probably doesn't have a noticeable performance hit since its only creating 10 a second (thanks to your Thread.Sleep(100);, I would suggest using the same one.
On a side note, I would recommend completely refactoring this code for a more object oriented approach. Create a class that holds a Texture2D and a Vector2. Then give it an Update() method and a Draw(SpriteBatch sb) method and perform your work in there.
That's just a suggestion though.
You can do something similar to this, and you should use the content manager to load assets.
public class Sprite
{
public Vector2 Position = Vector2.Zero;
public Texture2D Texture;
public float Scale = 1;
public void LoadAsset(Game game, string asset)
{
Texture = game.Content.Load<Texture2d>(asset);
}
public Draw(SpriteBatch batch)
{
batch.Draw(Texture, Position, null, Color.White, ...,... Scale,...);
}
}
//In your game:
List<Sprite> Sprites = new List<Sprite>();
Initialize()
{
base.Initialize();
Sprite myBox = new Box();
myBox.LoadAsset("Box");
Sprites.Add(myBox);
}
Update(GameTime gametime)
{
myBox.Position += Vector2.UnitX * Speed * (float) gametime.elapsed.TotalSeconds;
}
Draw()
{
batch.begin();
foreach (Sprite sprite in Sprites) sprite.Draw(batch);
batch.end();
}
Both good answers by Blau and justnS. I would also advise to take a look at some XNA tutorials to get a better understanding of the XNA Framework and how it should be used (i.e. the purpose of the separate Initialize(), LoadContent(), Update(), Draw(), etc methods).
Try these for starters:
Riemer's
MSDN
XNA Game Dev

Categories

Resources