Difficulties with 2D avatar - c#

I tried this tutorial but I get multiple error messages.
http://what-when-how.com/xna-game-studio-4-0-programmingdeveloping-for-windows-phone-7-and-xbox-360/2d-avatars-using-render-targets-xna-game-studio-4-0-programming/
I just want to draw a 2D avatar.
The name 'world' does not exist in the current context
The name 'view' does not exist in the current context
The name 'projection' does not exist in the current context
The name 'avatarRenderer' does not exist in the current context
The name 'avatarAnimation' does not exist in the current context
How can I fix the errors?
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
RenderTarget2D renderTarget;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
renderTarget = new RenderTarget2D(GraphicsDevice, 512, 512, false, SurfaceFormat.Color, DepthFormat.Depth16);
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 1, 0.01f, 200.0f);
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(Color.Transparent);
avatarRenderer.World = world;
avatarRenderer.View = view;
avatarRenderer.Projection = projection;
avatarRenderer.Draw(avatarAnimation);
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
spriteBatch.Draw(renderTarget, Vector2.Zero, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}

This tutorial is supposed to be added to a basic tutorial that already draws an Avatar. E.g. this one.
world, view and projection are the transformation matrices to be used. And the avatarRenderer is the renderer you can retrieve from an AvatarDescription.

Related

What am I doing wrong in this Monogame class?

What am I doing wrong in this class? I'm using monogame and C# but my object won't render in the program.
class Player : Game
{
Texture2D PlayerSprite;
Vector2 PlayerPosition;
public Player()
{
Content.RootDirectory = "Content";
PlayerSprite = Content.Load<Texture2D>("spr_Player");
PlayerPosition = Vector2.Zero;
}
public void Update()
{
}
public void Draw(SpriteBatch SpriteBatch)
{
SpriteBatch.Begin();
SpriteBatch.Draw(PlayerSprite,PlayerPosition, new Rectangle(0, 0, 32,32), Color.White);
SpriteBatch.End();
}
}
The Load, Update, and Draw methods belong to the inherited Game class and are overrided by it.
You too need to start your SpriteBacth object.
GraphicsDevice object has exist in Game main class.
Try this:
class Player : Game
{
Texture2D PlayerSprite;
Vector2 PlayerPosition;
SpriteBatch spriteBatch;
public Player()
{
Content.RootDirectory = "Content";
PlayerSprite = Content.Load<Texture2D>("spr_Player");
PlayerPosition = Vector2.Zero;
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void Update(GameTime gameTime)
{
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(PlayerSprite,PlayerPosition, new Rectangle(0, 0, 32,32), Color.White);
spriteBatch.End();
}
}

C# ArgumentNullException was unhandled (SpriteBatch.Draw)

I am actually a new student in XNA, finding this library very interesting, but I still lack some knowledge to go further as I felt on a issue I can't fix on my own :(
The spriteBatch.Draw() method says my texture is null, however I have loaded it in a Resources.cs class and passed the texture in MainMenu.cs, so I don't really know where the problem resides, if anybody could help me about that I would be very thankful !
Resources.cs
class Resources
{
public static Texture2D pixel;
public static Texture2D startButton, loadButton, quitButton;
public static SpriteFont consoleFont;
public static void LoadContent(ContentManager Content)
{
pixel = Content.Load<Texture2D>("Pixel");
consoleFont = Content.Load<SpriteFont>("Console");
// UI Ressources :
startButton = Content.Load<Texture2D>("UI/StartButton");
loadButton = Content.Load<Texture2D>("UI/LoadButton");
quitButton = Content.Load<Texture2D>("UI/QuitButton");
}
}
MainMenu.cs
class MainMenu
{
// Fields
List<Button> buttons = new List<Button>();
// Constructors
public MainMenu()
{
this.buttons.Add(new Button(new Vector2(480, 132), 256, 48, Resources.startButton));
this.buttons.Add(new Button(new Vector2(480, 212), 256, 48, Resources.loadButton));
this.buttons.Add(new Button(new Vector2(480, 292), 256, 48, Resources.quitButton));
}
// Methods
// Update
public void Update()
{
}
// Draw
public void Draw(SpriteBatch spriteBatch)
{
foreach (Button button in buttons)
{
button.Draw(spriteBatch);
}
}
}
Button.cs
class Button : UIElement
{
int width, height;
Texture2D texture;
public Button()
{
}
public Button(Vector2 b_position, int b_width, int b_height, Texture2D b_texture)
{
this.position = b_position;
this.width = b_width;
this.height = b_height;
this.texture = b_texture;
}
public void Update()
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
Simple realy
You just have to know the calling order of functions of Game1. First, Game1 constructor is called. That's where you've put mainMenu = new MainMenu();. After that go Initialize, Load and so on. You have to move mainMenu = new MainMenu(); from Game1 constructor to Load function, after the Resources.LoadContent(Content);, so the resources can be loaded before they are used in MainMenu. Your Game1.cs code should look like this:
public class GameMain : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
MainMenu mainMenu;
public GameMain()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.IsMouseVisible = true;
}
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);
// TODO: use this.Content to load your game content here
Resources.LoadContent(Content);
mainMenu = new MainMenu();
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
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.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
mainMenu.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
While I do not see your Game class my first assumption is that your never calling the public static void LoadContent(ContentManager Content) method inside your Resources class. Since this function is instantiating your textures perhaps this isn't being called.
Thank you for your reply !
Indeed I forgot to include the Game class, is this the right way to call the resources ?
public class GameMain : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
MainMenu mainMenu;
public GameMain()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.IsMouseVisible = true;
mainMenu = new MainMenu();
}
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);
// TODO: use this.Content to load your game content here
Resources.LoadContent(Content);
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
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.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
mainMenu.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}

XNA Sprite doesn't seem to be updating?

I've looked through some other questions and haven't found my answer so I'm asking you lovely people as you have helped me before :)
I have a main class (RPG.cs) and a player class (Player.cs) and I want the Player class to be as self-contained as possible (because I don't want a huuuge main class) but here is my problem.
Problem
My Player sprite draws fine but when I want it to move, it won't update! At the moment I'm attempting to make it act like a cursor to test movement but eventually I'll have it bound to WASD or the arrow keys. So I want my sprite to follow my mouse at the moment but it just stays at 50, 50 (it's preset starting position) Have I missed something obvious? I'm new to XNA and I've spent half an hour on this problem!
RPG.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace TestGame
{
public class RPG : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D PlayerTex;
Rectangle playerPos = new Rectangle(
Convert.ToInt32(Player.Pos.X),
Convert.ToInt32(Player.Pos.Y), 32, 32);
public RPG()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize() { base.Initialize(); }
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
PlayerTex = Content.Load<Texture2D>("testChar");
}
protected override void UnloadContent() { }
protected override void Update(GameTime gameTime)
{
if
(
GamePad.GetState(PlayerIndex.One)
.Buttons.Back == ButtonState.Pressed
)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
spriteBatch.Draw(PlayerTex, playerPos, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Player.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 TestGame
{
/// This is a game component that implements IUpdateable.
public class Player : Microsoft.Xna.Framework.GameComponent
{
public static Vector2 Pos = new Vector2(50, 50);
MouseState ms = Mouse.GetState();
public Player(Game game) : base(game) { }
/// 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() { base.Initialize(); }
/// Allows the game component to update itself.
public override void Update(GameTime gameTime)
{
Pos.X = ms.X;
Pos.Y = ms.Y;
base.Update(gameTime);
}
}
}
Hope you can help! :)
As mentioned in the comment, I am not going to solve every problem. But the main errors.
Let the player be responsible for its position, not the game. Furthermore, I would make the player responsible for drawing itself, but that goes a bit too far for this answer.
The following code should at least work.
public class Player : Microsoft.Xna.Framework.GameComponent
{
public Vector2 Pos { get; set; }
public Player(Game game) : base(game)
{
this.Pos = new Vector2(50, 50);
}
public override void Initialize() { base.Initialize(); }
public override void Update(GameTime gameTime)
{
var ms = Mouse.GetState();
Pos.X = ms.X;
Pos.Y = ms.Y;
base.Update(gameTime);
}
}
Then use the player in your game:
public class RPG : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D PlayerTex;
Player player;
public RPG()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
player = new Player(this);
Components.Add(player);
}
protected override void Initialize() { base.Initialize(); }
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
PlayerTex = Content.Load<Texture2D>("testChar");
}
protected override void UnloadContent() { }
protected override void Update(GameTime gameTime)
{
if
(
GamePad.GetState(PlayerIndex.One)
.Buttons.Back == ButtonState.Pressed
)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
spriteBatch.Draw(PlayerTex, player.Pos, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
I have removed the texture's size. Don't know if you really need this. If so, you can let Player expose a Rectangle, not just a Vector2.
The key solution which Nico included is just that you using the original rectangle coordinates you made when you draw:
Rectangle playerPos = new Rectangle(
Convert.ToInt32(Player.Pos.X),
Convert.ToInt32(Player.Pos.Y), 32, 32);
Here you make the rectangle using the players CURRENT starting time position.
Then you ALWAYS draw based on this rectangle you made ages ago and never updated:
spriteBatch.Draw(PlayerTex, playerPos, Color.White);
The right way as Nico mentioned is just to change the draw to this:
playerPos = new Rectangle(
Convert.ToInt32(Player.Pos.X),
Convert.ToInt32(Player.Pos.Y), 32, 32);
spriteBatch.Draw(PlayerTex, playerPos, Color.White);
Now you will be drawing at a new place every time. There are better ways to do this (like what nico did) but here is the core idea.

I am getting a null error

I am getting an error message in this line:
player = new Player(this.Content.Load<Texture2D>("Player"),
actor.position,
this.spriteBatch,
enemyManager);
which tells me the actor object is null. How can I overcome this situation?
The exact error is:
Field Shmup.MyGame.actor is never assigned to, and will always have its default value null.
Here is my code:
class MyGame // [edit:] added based on below constructor
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player player;
EnemyManager enemyManager;
Actor actor;
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
IsMouseVisible = true;
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);
startTheGame();
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
this.player.Update(gameTime);
this.enemyManager.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Maroon);
this.spriteBatch.Begin();
this.player.Draw(gameTime);
this.enemyManager.Draw(gameTime);
this.spriteBatch.End();
base.Draw(gameTime);
}
void startTheGame()
{
enemyManager = new EnemyManager(this.Content.Load<Texture2D>("Enemy"), new Vector2(16), this.spriteBatch);
player = new Player(this.Content.Load<Texture2D>("Player"),actor.position, this.spriteBatch, enemyManager);
enemyManager.StartTheGame(10);
}
}
public class Actor
{
public Texture2D texture;
public Vector2 origin;
public SpriteBatch spriteBatch;
public Vector2 position;
public Rectangle boundingRectangle;
public Vector2 Position
{
get { return position; }
set
{
position = value;
boundingRectangle = new Rectangle((Int32)(position.X - origin.X), (Int32)(position.Y - origin.Y), texture.Width, texture.Height);
}
}
public Rectangle BoundingRectangle
{
get { return boundingRectangle; }
}
public Actor(Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, Vector2 initialPosition)
{
this.texture = texture;
this.origin = origin;
this.spriteBatch = spriteBatch;
this.position = initialPosition;
boundingRectangle = new Rectangle((Int32)(initialPosition.X - origin.X), (Int32)(initialPosition.Y - origin.Y), texture.Width, texture.Height);
}
public virtual void Update(GameTime gameTime)
{ }
public virtual void Draw(GameTime gameTime)
{
this.spriteBatch.Draw(texture, position - origin, Color.White);
}
}
Like error says, you need to create an instance of Actor class.
In game constructor or in startTheGame Method(before creating an instance of Player class) you should create an instance of Actor class, something like :
this.actor = new Actor(variables...);

Error on xna "is inaccessible due to its protection level"

I am getting an error when I try to get the mouse position.
This is my player class. I am trying to make the player sprite move by mouse cursor
class Player : Actor
{
public MouseState mState;
EnemyManager enemyManager;
public Player(Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, EnemyManager enemyManager)
: base(texture, origin, spriteBatch, new Vector2(250.0f, 516.0f))
{
this.enemyManager = enemyManager;
}
public override void Update(GameTime gameTime)
{
//KeyboardState keyboardState = Keyboard.GetState();
//if (keyboardState.IsKeyDown(Keys.Left))
//{
// if (this.Position.X > 32.0f)
// {
// this.Position -= 10.0f * Vector2.UnitX;
// }
//}
//if (keyboardState.IsKeyDown(Keys.Right))
//{
// if (this.Position.X < 748.0f)
// {
// this.Position += 10.0f * Vector2.UnitX;
// }
//}
MouseState mState = Mouse.GetState();
this.Position = new Vector2(mState.x, mState.y);
// Collisions...
foreach (Enemy e in this.enemyManager.Enemies)
{
if (this.BoundingRectangle.Intersects(e.BoundingRectangle))
{
e.OnHit();
break;
}
}
base.Update(gameTime);
}
}
and this is my Actor class which has position variables
namespace Shmup
{
public class Actor
{
public Texture2D texture;
public Vector2 origin;
public SpriteBatch spriteBatch;
public Vector2 position;
public Rectangle boundingRectangle;
public Vector2 Position
{
get { return position; }
set { position = value; }
}
public Rectangle BoundingRectangle
{
get { return boundingRectangle; }
}
public Actor(Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, Vector2 initialPosition)
{
this.texture = texture;
this.origin = origin;
this.spriteBatch = spriteBatch;
this.position = initialPosition;
boundingRectangle = new Rectangle((Int32)(initialPosition.X - origin.X), (Int32)(initialPosition.Y - origin.Y), texture.Width, texture.Height);
}
public virtual void Update(GameTime gameTime)
{
}
public virtual void Draw(GameTime gameTime)
{
this.spriteBatch.Draw(texture, position - origin, Color.White);
}
}
}
my main class
public class MyGame : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player player;
EnemyManager enemyManager;
Actor actor;
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
IsMouseVisible = true;
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
Actor actor = new Actor();
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
startTheGame();
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
this.player.Update(gameTime);
this.enemyManager.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Maroon);
this.spriteBatch.Begin();
this.player.Draw(gameTime);
this.enemyManager.Draw(gameTime);
this.spriteBatch.End();
base.Draw(gameTime);
}
void startTheGame()
{
enemyManager = new EnemyManager(this.Content.Load<Texture2D>("Enemy"), new Vector2(16), this.spriteBatch);
player = new Player(this.Content.Load<Texture2D>("Player"),actor.position, this.spriteBatch, enemyManager);
enemyManager.StartTheGame(10);
}
}
This line is the problem I think:
this.Position = new Vector2(mState.x, mState.y);
x and y are not publicly exposed on MouseState. You need to capitalize them (C# is case-sensitive). Use this instead:
this.Position = new Vector2(mState.X, mState.Y);
"making these changes gives me a different error Object reference not set to an instance of an object in the line player = new Player(this.Content.Load("Player"),actor.position, this.spriteBatch, enemyManager); on my main class it says Error 'Shmup.Actor' does not contain a constructor that takes 0 arguments –"
The constructor for Actor requires - Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, Vector2 initialPosition .
So you would need to change the "Actor actor = new Actor();" into LoadContent() or a separate method after you have supplied all the information required to declare a new Actor.
It should read something along these lines(replace each argument with the proper item from your game class):
Actor actor = new Actor( texture, origin, spriteBatch, initialPosition);
If you do that it will fix the error, If you don't you will continue to get the error unless you define a constructor in Actor that takes 0 arguments. Hope this helps somewhat.

Categories