Jump to content
  • Advertisement
Sign in to follow this  
cryothic

[XNA] Best practice for starters?

This topic is 2519 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I've just started playing with XNA for a bit (WebDeveloper by profession), and just read some tutorials.

One thing I hate, but still see in most beginner-tutorials, is that they all just use the Game class to do all the work.
They make a class variable for a texture, and move it around, all in the same class. So, first thing I wanted to do, is follow a beginner tutorial, and try to "refactor" it a bit.

I've made a player.cs class. (I know, not the best name, but it's just for learning now ;))
In the constructor, if pass through a ContentName, and a reference to the game object itself (so I could make use of the ContentManager etc).

I've also made some movement classes. But there is where the problem appears...
How do I check if this sprite doesn't leave the viewport?

I can't get access to ((Game)Game1).graphics, so I can't get access to the preferredBackBufferHeight/Width.

What is the most efficient way to get access to this info?
Passing screenheight/width for every instance would be a bad thing.
And passing the GraphicsManager didn't seem to work.

Or should I create a GraphicsManager in this class? This might be very costly in runtime when there are a lot of instances, of am I wrong?


Some code to make things maybe a little more clearer:
Game1.cs

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Player myPlayer;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
myPlayer = new Player("Player", this);
base.Initialize();
}
...


Player.cs

public class Player
{
private Vector2 _position = new Vector2(0, 0);
private KeyboardState _keyboardState;
private Game _game;

public Player(String contentName, int stepSize, Game game)
{
this._game = game;
this._game.Content.Load<Texture2D>(contentName);

_keyboardState = Keyboard.GetState();

}

public void Move()
{
if (_keyboardState.IsKeyDown(Keys.Up)) this.MoveUp();
if (_keyboardState.IsKeyDown(Keys.Down)) this.MoveDown();
if (_keyboardState.IsKeyDown(Keys.Left)) this.MoveLeft();
if (_keyboardState.IsKeyDown(Keys.Right)) this.MoveRight();

if (_position.Y < 0) _position.Y = 0;
if (_position.X < 0) _position.X = 0;

//THIS IS WHERE I WOULD NEED MY LOGIC TO DETERMIN THE SCREENSIZE

if (_position.Y > graphics.PreferredBackBufferHeight - player.Height) position.Y = graphics.PreferredBackBufferHeight - player.Height;
if (_position.X > graphics.PreferredBackBufferWidth - player.Width) position.X = graphics.PreferredBackBufferWidth - player.Width;
///////////////////////////
}

...

Share this post


Link to post
Share on other sites
Advertisement
112 views, and 0 replies?

Am I the only one who stumbles upon this problem?

There must be more people around, who have an object class that can move, but can't leave the screen.
Or are they all doing the movement-boundries logic within the game itself?

Share this post


Link to post
Share on other sites
[color=#1C2837][size=2]And passing the GraphicsManager didn't seem to work.[/quote]
It works , maybe you tried to use it when the device wasn't created ?

Share this post


Link to post
Share on other sites

[color="#1C2837"]And passing the GraphicsManager didn't seem to work.

It works , maybe you tried to use it when the device wasn't created ?
[/quote]
You're right, must have made another error or something (it was created, since I created my "player-class" in the onLoad of the game.

But is that the correct solution? Passing the GraphicsDeviceManager, or the ScreenHeight/Width to every instance of a movable object?
It would seem to be more efficient to make them publicly available from within the Game class
Which probably mean I need to make an custom Base class for Game.

Share this post


Link to post
Share on other sites
After you pass Game to your player class you have access to the graphics device through Game.GraphicsDevice.

You can get access to the view port size through "GraphicsDevice.Viewport.Width" and "GraphicsDevice.Viewport.Height".

If you have other classes that you would like to have available through "Game," you might consider inheriting from GameComponent (or creating an interface to inherit from) and then adding the class to the "Game.Services" list and then just retrieve the class whenever you want with a call to "Game.Services.GetService".

Share this post


Link to post
Share on other sites
Im by no means an expert with XNA, but I've dabbled around alot and have made a fair few games (not all finished yet...)

your main problem here is that your creating your player in the Initialise method, this means that the graphics device will still be null, you need to make sure you Instantiate any classes you've made in the LoadContent() Method! The way I usually go about development in XNA, is I'll create a gui class/ menusystem class, I'll create it in the game1 class, making sure to call say... nameOfCreatedClass.Update(gameTime); and the same for the draw method, then in the constructor of the gui/menu class, I pass in the content manager from the game1 class, so that, when needed I can pass it down through the classes I create to load content dynamically as enemies/game objects are used.

With regards to figuring out where the player object/class is in relation to the bounds of the screen, I create a rectangle in the game1 class usually, called ScreenBounds, then in the load content method, just after the graphics device is created, I do something like, graphics.PreferredBackBufferWidth = ScreenBounds.Width; and the same for height, then if need be, you can also pass that same rectangle down into the menu/gui class so that you can use it,


Hope this helps a bit, and if your still not sure and you think I can help, then give me a message :P


Steven

Share this post


Link to post
Share on other sites
Thanks guys, those are some nice guidelines I can use.

Also searching for "GameComponent" tutorials was a nice addition. Didn't come across that term in the beginner tutorials.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!