this is my first question and I just started coding in C# some days ago, so please be kind, if I’m doing something stupid. The thing is, I wanna write a small 2D-game in xna and therefore I created a 32×32 px box with texture and that should be the player. I can use the arrow-keys to move the player by writing a class for the player like
namespace MyGame
{
class Player
{
public Texture2D Textur;
public Vector2 Position { get; set; }
}
}
and then using
KeyboardState keyboard = Keyboard.GetState();
if (keyboard.IsKeyDown(Keys.Right)) player.Position += new Vector(5,0);
//and so on
where 5 is the amount of pixels, the player moves with one keystroke.
What I’m trying is to write something like this
class Player
{
public Texture2D Textur;
public Vector2 Position { get; set; }
public Vector2 UpperRightCorner = new Vector2(Position.X + 32, Position.Y);
}
But obviously it doesn’t work. Even thought I read the documentation on msdn about get; set; I do not really understand, how it works.
I need the corners, or more precisely the pixel next to the corner, to write a collision method. This method should ask everytime, when I press an arrow-key, what kind of level-block is in front of the player and then adapt the speed resp. set the movement to 0, if there is a solid block (like a wall).
Any idea, how i can write this corner-stuff IN the player class? I can call it in the main class, after instancing the player, but I would prefer to put it into the player class.
You’re assigning a value to
UpperRightCornerwhen it is instantiated. This value will remain unchanged. This could be rewritten with a getter to recalculateUpperRightCornerevery time it is accessed. Example: