I've created two classes which replace the function of a Point
(both variants) just to test them out, this is how I've created them:
Entity.cs:
namespace Xnagame
{
class Player
{
public class float2
{
public float X { get; set; }
public float Y { get; set; }
}
public class int2
{
public int X { get; set; }
public int Y { get; set; }
}
public int2 Position { get; set; }
public int angle { get; set; }
public float speed { get; set; }
public float2 velocity { get; set; }
public Player CreatePlayer()
{
Position = new Player.int2();
Player player = new Player();
return player;
}
}
}
As you can see, both Point classes have a X
and Y
variable (I'm not sure if this is the way to set them up, if it isn't please tell me) and these classes are used in the position and velocity instances which are instantiated in the CreatePlayer()
method.
Game1.cs:
namespace Xnagame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Texture2D playerimage;
private Texture2D background;
Player player = Player.CreatePlayer();
Now the problem is that when CreatePlayer()
method tries to return it's "player" to the local player it gives the:
An object reference is required for the non-static field, method, or property
Xnagame.Player.CreatePlayer()
error.
I also tried it with the new keyword and it gave me:
Xnagame.Player.CreatePlayer()
is a method but is used like a type.
Your CreatePlayer
method is an instance method - in other words, it has to be called on an existing instance. You probably want to make it static:
public static Player CreatePlayer()
You should then remove the Position = new Player.int2()
line - creating a player shouldn't change the location of an existing player, presumably.
(I'd also strongly recommend extracting your nested types to be immutable top-level structs, and renaming them to something like Int32Position
and SinglePosition
- although you may well find that the framework has something similar already. You should also make all your properties follow .NET naming conventions.)
See more on this question at Stackoverflow