开发者

Should all initialization code be restricted to the Initialize method?

Alright, so I wanted to know if it's ok if I'm initalizing the object in the load content method instead of the initialize method? Is it really important? Thanks.

BTW the reason I'm asking this question is because that I have to load the texture before I intialize my player object, and I can't think of possible way to do it except this wa开发者_运维知识库y.

If anyone has an idea what should I do, It'd be great, thanks alot.


It is ok to create objects with new in the LoadContent method. You can load content everywhere else [after Game.LoadContent is called -AR], too.

Xna is only providing a pattern with there Initialize and LoadContent methods. If you want to keep it, add LoadContent method to your player object and call it from your Game LoadContent method (or use a drawable game component for your player).

Edit:

Here is an example using a DrawableGameComponent:

Player.cs

class Player : DrawableGameComponent
{
    public Player(Game game) : base(game)
    {
    }

    protected override void LoadContent()
    {
       // ... load your content via Game.Content.Load<...>(...);
    }
}

Game.cs

protected override void Initialize()
{
    Components.Add(new Player(this));
}


In my games, I don't ever user the initialize method. The reason for that is I actually find inconsistency in what methods get called and when. So in my LoadContent() method, I construct the object and the constructor loads what ever textures it needs in it own method that I hand it. So you will have the texture when the the player is done constructing.

It honestly doesn't matter what order you init or load content in, you should just be consistent because thats good code. (or easier to read)`

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜