Jump to content
  • Advertisement
Sign in to follow this  

couple of XNA questions

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

First question is on mouse clicks. I have a custom GUI_BUTTON and all i want it to do is when i click it .... to increment a variable. I then display that variable to the screen. so basically i have this

int count = 0;
bool pressed = false;

if (mouse is pressed)
pressed = true;
if (mouse is inside button bounds AND pressed)
increment count
pressed = false;

this makes the button work when i click inside of it. but when i click outside of it .... well thats diff. It doesnt incrememnt the count ... but when i click outside button then move mouse inside bounds .... it then increments. im not 100% sure how to make that stop. I know at some point i need reset pressed to false .... but ive tried putting that in a few spots and nothing seems to work. ill try and post code for this when i get home.

My next question is about fonts in XNA and C#.
I want to create a "GUI_LABEL" and have it basically have a series of strings like an array or something and have it display the current index of the array to the label.
so like

collection of strings called string_list
string_list.push_back("this is line 1");
string_list.push_back("this is line 2");
string_list.push_back("this is line 3");
string_list.push_back("this is line 4");
string_list.push_back("this is line 5");
// pardon syntax im like falling asleep as i write this and im sure its horrible /just trying to get the general idea out there

and so on.

then have a variable like

int current_line;
string current_string;

i want to be able to do something like

current_string = string_list[current_line]; again its the syntax thats killing me. im not too good with C# just yet and the difference of containers to C++ seems to throw me off.

and lastly ... lol sorry to be a pain
in my GUI_LABEL class would i need to pass a Spritefont object to draw text or some sort of SpriteBatch ... or should i just have a method that returns a string and leave all that to the draw method that is given to me when i create an XNA project.

Share this post

Link to post
Share on other sites
It really is straightforward. Create a global bool variable in your button class like "mouseDownInsideButton". When the mousebuttondown is registered inside the button, set that variable in the Update of the button to true. Then on the mousebuttonup you first check if mouseDownInsideButton is true. If no, cancel the action. If yes, perform the increment. Either way, set mouseDownInsideButton to false again.

It's just like you said. Except that I would use a list or an array.
List<string> allStrings = new List<string>();
allStrings.Add("this is line 1");
allStrings.Add("this is line 2");
allStrings.Add("this is line 3");
allStrings.Add("this is line 4");
allStrings.Add("this is line 5");

int lineIndex = 0;
string line1 = allSrings[lineIndex];

But getting an element of an array or even a List is actually one of the very first things you learn when going through a C# tutorial.

So there's the XNA question :D
Have you ever worked with WinForms or WPF? You specify a font in a property of a button. Why not do it like this? Create a SpriteFont-property in the button class and give it a default value, for example in the constructor. Second, create a public Draw method and give it a SpriteBatch parameter. In the Draw method, draw the button outlines, backgrounds and whatsoevers, and finally the string by using Spritebatch.DrawString. It requires the string to draw, a SpriteFont and some more things. Pass the SpriteFont from the property in the button. Call Draw of the button in the Draw of your main game and you should be good to go.

I hope I got your questions correctly..

Share this post

Link to post
Share on other sites
this is perfect thanks again. I rated you up again lol since you also helped me in the beginners forum.

For #3 thats what i kinda figured i had to do but when i tried i was getting weird results when trying to use a font in a class ... do i need to pass any type of variable in like a content manager or would including / importing be enough to be able to use it in my class (fonts that is)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!