Menu's in XNA

Started by
12 comments, last by falcon93 12 years, 9 months ago
[/lurk]

He's doing programming for a menu. While your input is appreciated, and very much correct, it probably doesn't apply to his current situation.

[lurk]
Advertisement

Sure it works under ideal circumstances, but if there is a bit of lag, and someone clicks even a little bit fast, it is not unreasonable to suggest that the user might click and have nothing happen. My suggestion makes that eventuality completely impossible. But I guess these days no one cares about proper coding, just get it working asap because F*** GOOD CODE.

Seriously though, I'm not saying the above code doesn't work, I'm saying it is not entirely robust to the level of certainty that you will probably want to guarantee in a game. If I click, I expect my gun to shoot, regardless of how quickly i released the mouse. If you only test for the current state of the mouse you might miss clicks that entirely occurred between the last game loop and the current.


First of all, you should remember that an XNA game runs on 50 fps on default, no matter how fast your computer is. That means that you have to hold down the mouse button for 0.02 seconds or shorter to be able to "miss" a click.

The proffesional way of checking a button press is by capturing events. However, for a simple menu with 3 buttons, there's really no reason so start coding custom events. Or maybe you propose that he should spend most of the development time on mouse events, instead of actually learning other more important things? Events is also an important thing to know when you start to move more into the advanced tracks, but I bet there's more valuable things to learn first.

Also, think about your "proper" coding... Did you learn the easiest basics first, and then moved on to the "proper" way when you understood the concepts, or did you start learning the "proper" way stright ahead? I mean, he's not supposed to copy-paste code from here. He's suposed to understand the concept of the code and then use our help for creating his own solution. That's the way of learning, not by pressing a ctrl combination twice. It's like you should start biking before you could walk...

First of all, you should remember that an XNA game runs on 50 fps on default, no matter how fast your computer is. That means that you have to hold down the mouse button for 0.02 seconds or shorter to be able to "miss" a click.


I've never worked with XNA. Excuse me. It has already been noted that it is just for a menu, but it is likely that his game will be using the same input handling that the menu uses, so making it robust before it causes problems is not a problem. In a perfect world everything runs smoothly at 50 or more frames per second, but, as I am sure you are keenly aware, this is not always the case. Ensuring that all input gets handled eventually is, what I would say, a very important thing to learn in programming, otherwise a naive programmer will just assume it will work properly, which isn't necessarily the case.

The proffesional way of checking a button press is by capturing events. However, for a simple menu with 3 buttons, there's really no reason so start coding custom events. Or maybe you propose that he should spend most of the development time on mouse events, instead of actually learning other more important things? Events is also an important thing to know when you start to move more into the advanced tracks, but I bet there's more valuable things to learn first.
[/quote]

You might not have noticed, but my post contained no mention of events, event handlers, or any such thing, though it does rely upon the existence of some way of detecting the exact moment that the mouse has been pressed in a separate thread. This is the result of my working with java, where you have no choice but to deal with it in that manner. If there is some confusion about what I meant, here is a brief clarification.

The following method of dealing with mouse input is as follows(in java, because that is what I work with):
public class MyMouse implements MouseListener {
boolean buttons[] = new boolean[8];
public MyMouse() {
for(int i=0; i<8; i++)
buttons = false;
}
public void mousePressed(MouseEvent e) {
buttons[e.getButton()] = true;
}
public void mouseReleased(MouseEvent e) {
buttons[e.getButton()] = false;
}
public boolean isMousePressed(int buttonCode) {
return buttons[buttonCode];
}
} // end class


This is how I originally started dealing with mouse input. As I went on, I noticed that in rare combinations of circumstances, you could click without the game knowing it. Everyone here is going to be like " "I use java" <--- there's your problem right there" but the fact is that any programming language on any platform is capable of lagging. I know because I have been using a computer since I was two and /everything/ can lag.

I also do not think that it is a problem to deal with these events, especially for simple input like this. I'm not saying he go and add mouse gestures or anything.

If this method of mouse input handling is absolutely nothing like anything possible in XNA, then I will submit that my method is not only unnecessary, but likely impossible. However, since I'm pretty sure that it applies, I will continue.

The modification I was recommending at the start was to take the previously mentioned mouse class and add a second set of booleans for checking not the current state of the mouse, but the state of the mouse since the last time you checked it. you would add a new boolean array, I call it "hasBeenPressed". Modify the mousePressed function to also set the appropriate slot in hasBeenPressed to true, and add a function to check the value of hasbeenpressed which also sets the checked index to false. Modifications follow:

public class MyMouse implements MouseListener {
boolean buttons[] = new boolean[8];
boolean hasBeenPressed[] = new boolean[8];
public MyMouse() {
for(int i=0; i<8; i++)
buttons = false;
for(int i=0; i<8; i++)
hasBeenPressed = false;
}
public void mousePressed(MouseEvent e) {
buttons[e.getButton()] = true;
buttons[e.getButton()] = true;
}
public void mouseReleased(MouseEvent e) {
buttons[e.getButton()] = false;
}
public boolean isMousePressed(int buttonCode) {
return buttons[buttonCode];
}
public boolean hasMouseBeenPressed(int buttonCode) {
boolean out = hasBeenPressed[e.getButton()];
hasBeenPressed[e.getButton()] = false;
return out;
} // end class


Optionally, you can also include a separate function that will reset all of the hasBeenPressed values to false. I have found it useful in the past. The point I am trying to make here is that this solution is not complex, and ensures that, as long as the thread(s) that is handling events isn't being starved, input is seen. (Though with this method it is still possible to construct a scenario where input is not handled, but with even a little bit of planning this can be reduced to completely negligible).

Also, think about your "proper" coding... Did you learn the easiest basics first, and then moved on to the "proper" way when you understood the concepts, or did you start learning the "proper" way stright ahead? I mean, he's not supposed to copy-paste code from here. He's suposed to understand the concept of the code and then use our help for creating his own solution. That's the way of learning, not by pressing a ctrl combination twice. It's like you should start biking before you could walk...
[/quote]

The method described above is precisely how I do and always have handled mouse position and input. When I first started programming games in java, I created a Mouse class which is not unlike the one above (but it handles mouse position and a few other things).

I don't understand the hostility here. Even if this is too complex for a beginner to understand (Which I still maintain it isn't), should he or any other newbie lurking here ever encounter a situation where input is not consistently being processed, maybe they will remember a flash of this discussion and be set off in the right direction to fix the problem. When introducing someone to code with potential issues, it is important to point out, even if only briefly, potential problems with the code to ensure that if they end up going in a direction where the code breaks with an increasing frequency, they understand why or at least have an idea of how to go about fixing it.
Sure, I havn't said there's anything wrong with the code you use. I just think it's unnecessary advanced for creating a menu with three buttons. Also, he was not just wondering how to capture mouse clicks, he was also asking about the buttons themselves. For the buttons click action, the two most common solutions is either mine, or the more advanced one which includes events.

So no offence, lets leave both our solutions standing here, and let him choose the one that fits him best smile.gif

This topic is closed to new replies.

Advertisement