Skip to main content
GameDev.net gamedev.net
🔒 Locked

Question about object orientation (in C# /XNA)

Started by gbankuti Jun 7, 2011 at 3:48 AM 0 replies 900+ views
Original Post
gbankuti
gbankuti
[font="Consolas"][font="Consolas"]

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

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

[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]

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

[font="Times New Roman"][/font][font="Consolas"][font="Consolas"][/font][/font]
TravisGesslein
TravisGesslein
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:
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;
}

}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.