Sign in to follow this  
gbankuti

Question about object orientation (in C# /XNA)

Recommended Posts

[font="Consolas"][size="2"][font="Consolas"][size="2"]

[/size][/font][/size][/font][size="3"][font="Calibri"]Hi,[/font][/size]

[font="Times New Roman"][size="3"][/size][/font][font="Calibri"][size="3"] [/size][/font]

[size="3"][font="Calibri"]For learningpurposes I am trying to create a class named “World”, this class containsparameters like gravity and wind speed. The next step is the creation of aclass named “Sprite”, this would the class for my player (with player position,health,…). What I would like to do is to “attach” the player to the world,i.e., when creating an instance of “Sprite” I want to refer to which “World” (whichinstance of World class) the “Sprite” belongs to. What is the best approach fordoing this? Would World be a parent class of Sprite? How do I list the worldinstance as a parameter in the constructor of the sprite class in a way that ithas to show to which world the sprite belongs to.[/font][/size]

[font="Times New Roman"][size="3"][/size][/font][size="3"][font="Calibri"]Thanks,[/font][/size]

[font="Times New Roman"][size="3"][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
Don't use a Sprite class as a representation for your player. Sprites are usually general-purpose 2D elements. Instead, create an "Entity" or "Actor" class which inherits from Sprite or contains a sprite (either way is fine) and use that class to represent your player That way, you can easily use your sprite class for other drawing related things too, like Tiles on your map.

As to your question:[code]
class World
{
Acfor player;
public void attachPlayer(Actor *p)
{
player = p;
p.belongsTo(this);
}
}

class Actor
{
private World world;
public void belongsTo(World* w)
{
world = w;
}

}
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this