I'm a c++ programmer trying out c#. I've done a lot with box2d in c++, but this is my first time with c#. So, I'm trying to make a simple game with farseer physics engine. When I try to compile my code (I'm using visual studio c# 2010 express and xna game studio 4.0), it stops in body.cs at IBroadPhase broadPhase = World.ContactManager.BroadPhase; With this error: nullreferenceexception was unhandled. I believe the problem is in my Player class, so here's the code for that:
public class Player : Entity
{
Vector2 position;
SpriteBatch spriteBatch;
Texture2D texture;
Rectangle rectangle;
Body body;
CircleShape shape;
Fixture fixture;
public Player()
{
// TODO: Construct any child components here
}
///
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
///
public override void Initialize(World world, SpriteBatch spriteBatch, Texture2D texture)
{
// TODO: Add your initialization code here
this.spriteBatch = spriteBatch;
this.texture = texture;
rectangle = new Rectangle(0, 0, 11, 14);
body = BodyFactory.CreateBody(world);
body.BodyType = BodyType.Dynamic;
body.Position = new Vector2(0, 0);
shape = new CircleShape(1.0f, 1.0f);
fixture = body.CreateFixture(shape);
base.Initialize(world, spriteBatch, texture);
}
///
/// Allows the game component to update itself.
///
/// <param name="gameTime" />Provides a snapshot of timing values.
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(texture, new Vector2(body.WorldCenter.X * 10, body.WorldCenter.Y * 10), rectangle, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
The farseer testbed runs fine, so I'm pretty sure my code is the problem, and not farseer. Let me know if you need to see more of my code. I've also posted this in the farseer forums, so If I get an answer there, I'll let you guys know. Thanks in advance.
Someone asked me to show the code of Game1.cs, and found the problem. The problem was that I had never constructed the world before initializing my player. It was fixed by adding world = new World(Vector2.Zero); before I initialized the player.
Related
I just want to add a background image in my c# monogame main menu. I already have a main menu with buttons and a working game. Just the background is missing.
This is a part of my code:
public void LoadAssets()
{
background = ScreenManager.Texture("background");
[...] //unimportant stuff for this problem
}
public void Draw(GameTime gameTime)
{
SpriteBatch spriteBatch= new SpriteBatch();
spriteBatch.Begin();
spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
spriteBatch.End();
foreach (var button in mButtons)
{
button.Draw(ScreenManager.mSprites);
}
}
I get the following error CS7036 C# There is no argument given that corresponds to the required formal parameter of "graphicsDevice" from "SpriteBatch.SpriteBatch(GraphicsDevice).
I included the image in content. I don't know where is my error.
Thanks, for help!
First, you do NOT create a new SpriteBatch instance each draw call. That would be like 60 new instances per second (at 60fps)
Instead, you create it in your LoadContent() method and use it all the way in your Draw()
method:
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
[...]
}
Second, GraphicsDevice is definetly available in your Draw-Call as long as you have not messed something up really bad ;)
I'm running into an issue where I cannot seem to get any of my textures to be recognized anywhere outside of the loadContent method.
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Texture2D tileStart = Content.Load<Texture2D>("tile_start");
Texture2D tileCrossJunction = Content.Load<Texture2D>("tile_crossjunction");
Texture2D tileTJunction = Content.Load<Texture2D>("tile_t-junction");
Texture2D tileCorner = Content.Load<Texture2D>("tile_corner");
Texture2D tileHallway = Content.Load<Texture2D>("tile_hallway");
Texture2D tileDeadEnd = Content.Load<Texture2D>("tile_deadend");
Texture2D sqrPlayer = Content.Load<Texture2D>("sqr_player");
Texture2D sqrBaddieSmall = Content.Load<Texture2D>("sqr_baddie_small");
Texture2D sqrBaddie = Content.Load<Texture2D>("sqr_baddie");
Texture2D sqrBaddieLarge = Content.Load<Texture2D>("sqr_baddie_large");
}
No problems in this method, but when I try to reference any of these textures in my Draw method...
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.DarkGray);
base.Draw(gameTime);
spriteBatch.Begin();
spriteBatch.Draw(tileStart, new Vector2(0,0), Color.White);
spriteBatch.End();
}
I get the error "tileStart does not exist in the current context."
Normally, I would say that it isn't being recognized because tileStart is a variable being declared within the LoadContent method and therefore wouldn't be usable anywhere else. The reason I'm confused is that every tutorial I've read shows this exact syntax and it seems to work fine in those cases, so clearly there's something else going on here that I don't understand.
Any help you guys can provide would be greatly appreciated, thanks.
In C#, the "scope" of variables is well defined. In your code, the textures are created within the scope of the method "LoadContent" and then are deleted once the method is done. What you need to do is place the textures at a "class" level like so:
//outside of the method, and in general, should be placed near the top of the class
Texture2D tileStart;
Texture2D tileCrossJunction;
Texture2D tileTJunction;
Texture2D tileCorner;
Texture2D tileHallway;
Texture2D tileDeadEnd;
Texture2D sqrPlayer;
Texture2D sqrBaddieSmall;
Texture2D sqrBaddie;
Texture2D sqrBaddieLarge;
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
//be sure to remove Texture2D from these
//this will insure that the "class" level variables are called
tileStart = Content.Load<Texture2D>("tile_start");
tileCrossJunction = Content.Load<Texture2D>("tile_crossjunction");
tileTJunction = Content.Load<Texture2D>("tile_t-junction");
tileCorner = Content.Load<Texture2D>("tile_corner");
tileHallway = Content.Load<Texture2D>("tile_hallway");
tileDeadEnd = Content.Load<Texture2D>("tile_deadend");
sqrPlayer = Content.Load<Texture2D>("sqr_player");
sqrBaddieSmall = Content.Load<Texture2D>("sqr_baddie_small");
sqrBaddie = Content.Load<Texture2D>("sqr_baddie");
sqrBaddieLarge = Content.Load<Texture2D>("sqr_baddie_large");
}
Once you have done that, the "scope" of the variables will be at the class level, and you will be able to use them from other methods within the class.
In other words, there is no way to access a variable that is declared within a method from outside of that method (without passing it as a parameter to another method of course), the tutorials you are looking at might just be short hand, and expecting you to do it "properly".
I've been fighting with this problems for days now, browsing through the net, yet nothing helped me solve it: I'm creating a MonoGame application on Visual Studio 2012, yet when trying to load a texture I get the following problem:
Could not load Menu/btnPlay asset!
I have set content directory: Content.RootDirectory = "Assets"; Also the file btnPlay.png has properties set: Build Action: Content and Copy to Output directory: Copy if newer.
My constructor and LoadContent functions are totally empty, but have a look yourself:
public WizardGame()
{
Window.Title = "Just another Wizard game";
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Assets";
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
_spriteBatch = new SpriteBatch(GraphicsDevice);
Texture2D texture = Content.Load<Texture2D>("Menu/btnPlay");
_graphics.IsFullScreen = true;
_graphics.ApplyChanges();
}
I would be glad for any help! I'm totally desperate about the problem....
Under VS2012, Windows 8 64-bits and latest MonoGame as of today (3.0.1) :
create a subfolder named Assets
set Copy to Output to anything else than Do not copy
prepend assets to your texture path when loading it
namespace GameName2
{
public class Game1 : Game
{
private Texture2D _texture2D;
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
_texture2D = Content.Load<Texture2D>("assets/snap0009");
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(_texture2D, Vector2.Zero, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Here's your texture drawn :D
Note:
By convenience I kept the original value that the content's root directory points to : Content.
However, you can also directly specify Assets in the path:
Content.RootDirectory = #"Content\Assets";
Then load your texture without prepending Assets to its path:
_texture2D = Content.Load<Texture2D>("snap0009");
I have 2 sprites which when drawn together make up the correct image on the screen. Drawing them both at the same time is not an option.
Imagine this class:
class MyImage
{
Vector2 drawOffset; // this gets added before the image is drawn
Vector2 sourceRect; // this is where it is on the source texturepage
void Draw(Vector2 position)
{
position = position + drawOffset;
spriteBatch.Draw(sourceTexture, position, sourceRect, Color.White);
}
}
And this code calling into it:
MyImage a = new MyImage(); // assume they get initialised correctly
MyImage b = new MyImage(); // with different drawOffsets and sourceRects
a.Draw(position); // this composes the final
b.Draw(position); // screen image from the 2 source images
Now I'd like to add scale and rotation to the Draw() function, but am having real trouble getting the parameters to the SpriteBatch.Draw function correct. This would be the version which takes scale, rotation and an origin. I need the final composed image to scale and rotate correctly (around some arbitrary centre) but can't for the life of me work out how to manipulate the scale, rotation and origin parameters to make the 2 images appear to scale and rotate in concert. Has anyone done something like this before? Happy to mod the question based on feedback if anything's unclear. If images would help I can get them posted somewhere...
I've looked at rotation around point xna 2D but am still stumped.
Cheers,
Charlie.
Thanks so much for the answer below - using it I've managed to get the images rendering correctly. One other issue remains, which is that I seem to need to use a lot of spritebatch.Begin/End pairs (one per image render). I don't have a way to measure performance on this device yet and the framerate's not chugging so I guess it's not a problem.
Here's my code:
// gr is the graphic object:
// gr.position is the location of the image in the atlas
// gr.DrawOffset is the draw offset so the image is placed correctly in it's virtual box
// gr.pageIndex is the index into the texture/atlas array
// hw,hh are half the width/height of the image (it always rotates around it's centre in fact)
Matrix m = Matrix.CreateTranslation(-hw, -hh, 0) *
Matrix.CreateRotationZ(rotation) * // rotation : parameter
Matrix.CreateScale(scale) * // scale : parameter
Matrix.CreateTranslation(pos.X + hw, pos.Y + hh, 0); // pos : parameter!
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, m);
spriteBatch.Draw(page[gr.pageIndex].texture, gr.DrawOffset, gr.position, color);
spriteBatch.End();
If you are going to work with the SpriteBatch.Draw to draw the textures I would suggest that you forgo trying to manipulate the origin, scale arguments to try an achieve this, simply I doubt it can be done this way. But you do have an alternative, you can manipulate the SpriteBatch Matrix.
Here is a quick and dirty example, note that the texture I used here is 128x96 so I hard coded the values for that image size. Do not look for any best practices in this code, I wrote it to try and show the concept as cleanly as possible.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Texture2D _texture;
private MyImage _image1;
private MyImage _image2;
// Attributes of the composed sprite
private float _angle = 0.0f;
private Vector2 _position = new Vector2(100, 100);
private Vector2 _rotationPoint = new Vector2(96, 48);
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
_texture = Content.Load<Texture2D>("Gravitar");
// Create the two MyImage instances
_image1 = new MyImage(_texture, Vector2.Zero, Vector2.Zero);
_image2 = new MyImage(_texture, new Vector2(64, 0), new Vector2(64, 0));
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
_angle += 0.5f * elapsedTime;
if (Mouse.GetState().LeftButton == ButtonState.Pressed)
{
_angle = 0.0f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Left))
_position += new Vector2(-10, 0)*elapsedTime;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
_position += new Vector2(10, 0) * elapsedTime;
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);
// Setup the sprite batch matrix
// Notice that we first translate to the point or rotation
// then rotate and when we translate to the desired position we
// need to compensate for the first translation so that the texture
// appears at the correct location
Matrix m =
Matrix.CreateScale(1.5f)
* Matrix.CreateTranslation(-_rotationPoint.X, -_rotationPoint.Y, 0)
* Matrix.CreateRotationZ(_angle)
* Matrix.CreateTranslation(_position.X + _rotationPoint.X, _position.Y + _rotationPoint.Y, 0);
// Begin the SpriteBatch passing the matrix
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, m);
_image1.Draw(spriteBatch);
_image2.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
class MyImage
{
Vector2 _drawOffset;
Vector2 _sourcePoint;
Texture2D _sourceTexture;
public MyImage(Texture2D sourceTexture, Vector2 sourcePoint, Vector2 drawOffset)
{
_drawOffset = drawOffset;
_sourcePoint = sourcePoint;
_sourceTexture = sourceTexture;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_sourceTexture, _drawOffset,
new Rectangle((int)_sourcePoint.X, (int)_sourcePoint.Y, 64, 96),
Color.White);
}
}
}
}
I'm working on a 3D Spaceship game with XNA 3.1
I tried to separate my painting code from the game logic (altought XNA makes this almost).
I'm using a special static class that paints my models on screen... the main Game class uses this code at painting:
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.Black);
// Draws the stage (the skybox and objects like rocks)
stageManager.Draw(gameTime, GraphicsDevice, camera);
// Draws each player and each Enemy on stage given the camera
foreach (Player p in players)
p.Draw(camera);
foreach(Enemy e in enemies)
e.Draw(camera);
if(Configuration.Debug)
col.renderColBoundings(GraphicsDevice, camera);
GraphicHelper.drawOverlayText("50", "10"); // "Error" line...
base.Draw(gameTime);
}
But when I paint text, something weird occurs... Here's an image (original):
As you can see, everything looks overlapped (but in place)... Like the spaceship turbines.
The code inside GraphicHelper.drawOverlayText is this:
public static void drawOverlayText(String p1Hp, String currEnemies)
{
string text1 = "Player1: " + p1Hp;
string text2 = "Enemies: " + currEnemies + "/10";
string text3 = "Space Hogs Beta";
spriteBatch.Begin();
// Draw the string twice to create a drop shadow, first colored black
// and offset one pixel to the bottom right, then again in white at the
// intended position. This makes text easier to read over the background.
spriteBatch.DrawString(font, text1, new Vector2(651, 11), Color.Gray);
spriteBatch.DrawString(font, text1, new Vector2(650, 10), Color.White);
spriteBatch.DrawString(font, text2, new Vector2(851, 11), Color.Gray);
spriteBatch.DrawString(font, text2, new Vector2(850, 10), Color.White);
spriteBatch.DrawString(font, text3, new Vector2(741, 611), Color.Gray);
spriteBatch.DrawString(font, text3, new Vector2(740, 610), Color.White);
spriteBatch.End();
}
And this static class has this attributes:
static ContentManager content;
static GraphicsDevice graphics;
static Camera camera;
static Dictionary<String, Model> models = new Dictionary<string, Model>();
static SpriteBatch spriteBatch;
static SpriteFont font;
public static void initHelper(ContentManager c, GraphicsDevice g, Camera cam)
{
content = c;
graphics = g;
camera = cam;
spriteBatch = new SpriteBatch(g);
font = c.Load<SpriteFont>("Fonts/main");
}
Hope you can help me :)
The odd rendering you are seeing is because the depth buffer is turned off. It gets turned off when you use SpriteBatch. This is a known oddity of the XNA 3.1 API. XNA 4.0 at least makes it a bit more obvious that this is happening.
Here is an explanation of what render states are changed by SpriteBatch in XNA 3.1. And here is the same thing for XNA 4.0.
The solution is to basically set your render states back to what you want them to be, after using SpriteBatch. In this case, at least, set:
GraphicsDevice.RenderState.DepthBufferEnable = true;
(There may also be some other states in there you want to change back.)