Jump to content
  • Advertisement
Sign in to follow this  
gbankuti

Question about object orientation (in C# /XNA)

This topic is 2544 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

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

Share this post


Link to post
Share on other sites
Advertisement
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;
}

}

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!