benson81

Hello,

I'm just starting out with XNA (and it's the first language I've tried that doesn't include the word basic...) but have found myself a bit stuck whilst trying to use GameComponents.

I followed the rather excellent tutorial for creating a tile engine at XnaResources and now have a fully functioning game component that draws & controls my tile map to screen - my idea being to create a simple 2D RPG, but first I want to build a map creator/editor that uses the tile engine to display & scroll the map.

So far so good - I've written a (very) basic interface that allows me to enter the name of the text file that stores the map data and then displays it on the screen. But where I've hit a wall is that anything I do in my main game is drawn over by the map being displayed by the tile engine...

For example, I want to display text at the top of my screen showing the map name but it doesn't appear and I can only presume the Draw method of the component is called after the Draw method of my main game class therefore "overwriting" it

So basically what I'm asking is how do I know what order the Draw methods get called Stepping through the code doesn't actually step into the Draw methods so I can't tell when each one is called. No doubt I'm missing something a bit obvious here but it's quite annoying as I'd really like to create multiple components to use and I don't want to have to keep amending the actual tile engine itself

Could someone please try & explain how this works as I'm completely stuck at the moment...maybe there's an element of running before walking here but I'd really like to understand this as it seems rather fundamental to the whole thing...


Re: XNA Game Studio Express Game Components - help!

Jim Perry

If you want something to draw after the component, put it after the DrawComponents(); call in the Draw method.




Re: XNA Game Studio Express Game Components - help!

benson81

Hi,
Thanks for your answer but I don't have any DrawComponents call in my main draw method. That's what's confusing me, I can't see anywhere that tells the component to draw itself.

My draw method is just this:
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

//Update display
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

//Display Text
WriteText(sTextDisplay, iTextDisplayX, iTextDisplayY, Color.Black);
WriteText(sTextUserInput, iTextUserInputX, iTextUserInputY, Color.White);

spriteBatch.End();

base.Draw(gameTime);
}

There is no DrawComponents() method in the component, is it incorrect that there is a Draw() method in the component As I said I copied this code from a tutorial so perhaps it wasn't designed to be integrated into other projects in this way




Re: XNA Game Studio Express Game Components - help!

benson81

Fixed - just needed to rename my Draw method in the component to DrawMap and call that in my main Draw method. Makes a lot more sense to me now.

Cheers




Re: XNA Game Studio Express Game Components - help!

dczraptor

base.Draw(gameTime) calls void Draw(GameTime gameTime) in all of your components. Don't rename your Draw method in the component. Just let the program do base.Draw(gameTime) to call your component. You can override the DrawOrder property of your component. A larger DrawOrder value will allow it to be drawn first, and a smaller value will be drawn last.




Re: XNA Game Studio Express Game Components - help!

benson81

That does make more sense than my messy workaround, however I still can't get it working that way. I've tried setting the DrawOrder to 10, 1000, 1000000 and still it overwrites any text I add in my main game class...

I can follow the logic that each component has it's own draw order, but how do I specify that I want the component to do it's drawing before my main class which isn't a component I can't find any draw order property for my main class.

Is it that I have to create additional components to do all the other drawing and then set their draw orders

Thanks for your help




Re: XNA Game Studio Express Game Components - help!

dczraptor

Oh yea, i forgot to mention that DrawOrder only applies to the components. One workaround you could do is just put your main text drawing into it's own component, and then set it's draw order to whatever value you want.




Re: XNA Game Studio Express Game Components - help!

benson81

Thanks - doing that now, means changing a fair bit of my code but it should keep things neater in the end.

No doubt would've got pretty messy trying to do everything from the main game class anyway.

Cheers