13 Replies - 1815 Views - Last Post: 16 August 2007 - 02:29 PM Rate Topic: -----

#1 pspfreak101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-July 07

Menu problem

Post icon  Posted 13 August 2007 - 07:01 AM

At first i was having trouble with it freezing cause of the loop but that problem is fixed. Now when I push down the selector doesn't move it still has to be with the loop, I made this in xna game studieos. also if someone could tell me how I can run the game script becuae i have the menu and the actually game in two scripts thanks

#region Using Statements
using System;
using System.Threading;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

	public class Menu : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		ContentManager content;
		bool selection1;
		bool selection2;
		bool selection3;
		bool credits;
		int selc = 0;
		
		public Menu()
		{
			graphics = new GraphicsDeviceManager(this);
			content = new ContentManager(Services);
			//sets window hight to psp size
			graphics.PreferredBackBufferHeight = 272;
			graphics.PreferredBackBufferWidth = 580;

			graphics.ApplyChanges();
		
		}
		//Game textures
		Texture2D menu;
		Texture2D selector;
		//X and Y positions
		Vector2 menuPosition = new Vector2(0, 0);
		Vector2 selectorPosition = new Vector2(215, 140);
		// This is the object that will draw the sprites.
		SpriteBatch spriteBatch;
		protected override void LoadGraphicsContent(bool loadAllContent)
		{
			if (loadAllContent)
			{
				menu = content.Load<Texture2D>("menu");
				selector = content.Load<Texture2D>("selector");
				spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
			}
		}

		protected override void Update(GameTime gameTime)
		{
			KeyboardState newState = Keyboard.GetState();
			KeyboardState oldState;
			oldState = Keyboard.GetState();

			while (newState.IsKeyDown(Keys.Down) && oldState.IsKeyUp(Keys.Down))
			{
				selc++;

				if (selc == 0)
				{
					selection1 = true;
					selc = 0;
				}

				if (selc == 1)
				{
					selection2 = true;
					selectorPosition.Y = selectorPosition.Y + 40;
					selc = 1;
				}

				if (selc == 2)
				{
					selection3 = true;
					selectorPosition.Y = selectorPosition.Y + 40;
					selc = 2;
				}

				if (selection1 == true)
				{
					if (newState.IsKeyDown(Keys.Enter))
					{
						//Run Game here
					}
				}

				if (selection2 == true)
				{
					if (newState.IsKeyDown(Keys.Enter))
					{
						credits = true;
					}
				}

				if (selection3 == true)
				{
					if (newState.IsKeyDown(Keys.Enter))
					{
						this.Exit();
					}
				}
				base.Update(gameTime);
			}
		}
	   
	   protected override void Draw(GameTime gameTime)
		{
			graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
			spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
			spriteBatch.Draw(menu, menuPosition, Color.White);
			spriteBatch.Draw(selector, selectorPosition, Color.White);
			spriteBatch.End();
			base.Draw(gameTime);
		}
	}

This post has been edited by pspfreak101: 13 August 2007 - 07:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Menu problem

#2 pspfreak101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-July 07

Re: Menu problem

Posted 14 August 2007 - 04:32 PM

WTF people every time I make a thread here no 1 ever replies
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Menu problem

Posted 14 August 2007 - 04:46 PM

Well you got to be patient on something like this. You are asking a very specific question and the people that would respond would have to be familiar with the framework you are using. Not everyone is going to know XNA. Perhaps you can post this in the game programming forum and get a better response? :)
Was This Post Helpful? 0
  • +
  • -

#4 pspfreak101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-July 07

Re: Menu problem

Posted 14 August 2007 - 05:21 PM

I tried the xna forums which helped on the freezing but after that knowone else responded :(
Was This Post Helpful? 0
  • +
  • -

#5 serializer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 25-June 07

Re: Menu problem

Posted 14 August 2007 - 05:40 PM

Well, hey, maybe it's just the way you phrase your questions. For instance, you began your post:

View Postpspfreak101, on 13 Aug, 2007 - 07:01 AM, said:

At first i was having trouble with it freezing cause of the loop but that problem is fixed. Now when I push down the selector doesn't move it still has to be with the loop, I made this in xna game studieos. also if someone could tell me how I can run the game script becuae i have the menu and the actually game in two scripts thanks


Now I'm thinking (and bear in mind this is kind of a commentary on my thoughts as I read your post): Trouble! Sounds bad.. It was freezing. What was freezing? What loop? Oh, it's fixed. Nevermind. Oh, you're pushing down a selector. What selector? Why are you pushing it? Is "down" the negative vertical axis on a 2D display, the negative Y axis in a 3D coordinate system, or the Z axis such as when you push a button on the screen? ... OMG ... it doesn't move because it still has to be with the loop? Is this the "loop", the very same loop that, for instance, you are "out of" when you don't understand what's going on? Ah, XNA, finally something I know about. Unfortunately you've completely lost me on the last sentence.

This is a forum where people will take their time and trouble to help you solve coding problems. The very least they expect is for you to ask a reasonable question, rather than just "OMG what's happening please fix my code otherwise I'll shout WTF".

--serializer
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Menu problem

Posted 14 August 2007 - 06:02 PM

Very well put serializer!
Was This Post Helpful? 0
  • +
  • -

#7 pspfreak101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-July 07

Re: Menu problem

Posted 15 August 2007 - 07:11 AM

View Postserializer, on 14 Aug, 2007 - 05:40 PM, said:

Well, hey, maybe it's just the way you phrase your questions. For instance, you began your post:

View Postpspfreak101, on 13 Aug, 2007 - 07:01 AM, said:

At first i was having trouble with it freezing cause of the loop but that problem is fixed. Now when I push down the selector doesn't move it still has to be with the loop, I made this in xna game studieos. also if someone could tell me how I can run the game script becuae i have the menu and the actually game in two scripts thanks


Now I'm thinking (and bear in mind this is kind of a commentary on my thoughts as I read your post): Trouble! Sounds bad.. It was freezing. What was freezing? What loop? Oh, it's fixed. Nevermind. Oh, you're pushing down a selector. What selector? Why are you pushing it? Is "down" the negative vertical axis on a 2D display, the negative Y axis in a 3D coordinate system, or the Z axis such as when you push a button on the screen? ... OMG ... it doesn't move because it still has to be with the loop? Is this the "loop", the very same loop that, for instance, you are "out of" when you don't understand what's going on? Ah, XNA, finally something I know about. Unfortunately you've completely lost me on the last sentence.

This is a forum where people will take their time and trouble to help you solve coding problems. The very least they expect is for you to ask a reasonable question, rather than just "OMG what's happening please fix my code otherwise I'll shout WTF".

--serializer


I'm sorry if i can't explain things right :blink: I thought my problem was easy to understand, think oh its a menu, hmm what the hell is a selector, well you have choices cause its a menu and you select things. As for the loop thiers only 1 LOOP!!! and its sure as hell not 3-D. So let me explain my problem
This is my menu

Posted Image

As you can see its very simple, you have 3 choices, start, options, and exit (you should know what those are for :P ) The selector is a red bar that selects what choice your on. When I push down I want the selector to go down and later when the while loop works I will have the selector go up. The selection is what you picked selection1 starts the game, selection2 is options, selection3 is exit Now the problem when I push down the selector doesn't go down. whats causing the problem? its the loop for sure ethier the loop isn't being true so none of the things in the loop work. How do I fix this? this is where you guys come in. Hope this explains things better I thought by looking at my code you could tell what I was trying to do.

This post has been edited by pspfreak101: 15 August 2007 - 07:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 serializer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 25-June 07

Re: Menu problem

Posted 15 August 2007 - 12:22 PM

Ok... your problem is very easy to understand now you've actually shown me what your code is trying to do. You've also been more specific about exactly what the problem is and where/when it occurs. My problem with your original post was mainly that it lacked these details. So you see, you can explain things right ;)

The key problem in your code is here:


			KeyboardState newState = Keyboard.GetState();
			KeyboardState oldState;
			oldState = Keyboard.GetState();

			while (newState.IsKeyDown(Keys.Down) && oldState.IsKeyUp(Keys.Down)) {




Follow the code through. newState and oldState BOTH get the value of Keyboard.GetState() assigned to them. Therefore they both actually just indicate the CURRENT state of the keyboard. Therefore it is impossible for the while() statement to be true: you can never have a key down AND up at the same time.

Instead, oldState should be a property of your Menu class:


	public class Menu : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		ContentManager content;
		bool selection1;
		bool selection2;
		bool selection3;
		bool credits;
		int selc = 0;
		KeyboardState oldState;




Then: at the END of your Update() method, assign oldState = newState. REMOVE the two oldState lines from the BEGINNING of the Update() method. Finally, in your Menu() constructor function, initialise oldState with oldState = Keyboard.State, so that when Update() runs it won't throw a null reference exception.

Your code will still not work properly for these two reasons:

1. A while() statement is a LOOP and will keep running as long as its conditions are returning TRUE. Is this really the effect you desire?
2. You are testing for the "Enter" key INSIDE the test for the "Down" key. Are you intending that "Enter" should only work WHILST "Down" is pressed?

Couple more things that seriously reduce the amount of code:

a. selection1, selection2, selection3 are redundant: you already have the selection number in selc. So replace if (selection1==true) with if(selc==0), etc. Delete any lines relating to selection1, 2, 3.
b. You test if(selc==0) then you set selc=0. If selc is already equal to 0, why set it again?

If you're not sure what to do about (1) or (2) above, come back with your updated code and describe what is now happening.

--serializer
Was This Post Helpful? 0
  • +
  • -

#9 pspfreak101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-July 07

Re: Menu problem

Posted 15 August 2007 - 06:49 PM

Thanks I think I'm not going with the loop still having problems with the never ending loop of death. I'm sure I'll find a differnt way to make a menu Can someone show me a simple menu example cause everyway I try to do it don't work cause of the selc++;

This post has been edited by pspfreak101: 15 August 2007 - 07:29 PM

Was This Post Helpful? 0
  • +
  • -

#10 serializer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 25-June 07

Re: Menu problem

Posted 15 August 2007 - 09:13 PM

Yes. Your problem is the while() loop. Read my post above. The Update() function IS your main loop, there's no need to have another loop within it. (Not for this simple task at least).

Your code nearly works. There's no need for anyone to post an example, when I've already offered to help fix your your existing code. Nobody is going to do the work for you. Maybe the reason people stopped replying in the XNA forums was because you took no notice of anything they said there?

--serializer
Was This Post Helpful? 0
  • +
  • -

#11 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Menu problem

Posted 15 August 2007 - 09:52 PM

I wanted to point out another thing that I see in the code. You set selc to zero when you initialize it then at the beginning of your while loop you increment it. Unless you set selc to -1 somewhere we don't see, selc will never be equal to zero in your first if test. Making it useless. selection1 will never get set to true.

Just something I noticed but I think serializer is giving you some very good advice.
Was This Post Helpful? 0
  • +
  • -

#12 serializer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 25-June 07

Re: Menu problem

Posted 16 August 2007 - 05:50 AM

View PostMartyr2, on 15 Aug, 2007 - 09:52 PM, said:

I wanted to point out another thing that I see in the code. You set selc to zero when you initialize it then at the beginning of your while loop you increment it. Unless you set selc to -1 somewhere we don't see, selc will never be equal to zero in your first if test. Making it useless. selection1 will never get set to true.


I think that's meant to happen: selc indicates which menu option is selected. It starts on zero, then as soon as the down arrow is pressed, it gets incremented. What he needs is to add a test for the up arrow, where selc is decremented, so it can be moved back to zero if needed.

@pspfreak101: try "if" instead of "while". You only want that test running *once* per Update() call.

--serializer
Was This Post Helpful? 0
  • +
  • -

#13 pspfreak101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-July 07

Re: Menu problem

Posted 16 August 2007 - 07:01 AM

This is the problem selectorPosition.Y = selectorPosition.Y + 40; cause when you press down once it supposed to go down to options but instead it skips to exit that and can't figure out how to fix it I did everything you said but thats why I asked for a example cause I'm stumped with this method.
-------------------------------------------------------------------------------------------------------------------------------------
Well I just converted some of my old lua code

protected override void Update(GameTime gameTime)
		{
			KeyboardState newState = Keyboard.GetState();
		   
			if (newState.IsKeyDown(Keys.Up))
			{
				if (upPressed == false)
				{
					if (selc > 140)
					{
						selc = selc - 40;
						upPressed = true;
					}
					else
					{
						selc = 200;
						upPressed = false;
					}
				}
			}

			if (newState.IsKeyDown(Keys.Down))
			{
				if (downPressed == false)
				{
					if (selc < 200)
					{
						selc = selc + 40;
						downPressed = true;
					}
					else
					{
						selc = 150;
						downPressed = false;
					}
				}
			}
			base.Update(gameTime);
		}  
needs a little work

This post has been edited by pspfreak101: 16 August 2007 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

#14 serializer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 25-June 07

Re: Menu problem

Posted 16 August 2007 - 02:29 PM

View Postpspfreak101, on 16 Aug, 2007 - 07:01 AM, said:

This is the problem selectorPosition.Y = selectorPosition.Y + 40; cause when you press down once it supposed to go down to options but instead it skips to exit that and can't figure out how to fix it I did everything you said but thats why I asked for a example cause I'm stumped with this method.


Yup ... and I knew this was going to happen ... read my posts again! This is why I pointed out the while() would cause a problem, and it's why I said "come back with your updated code and describe what's happening". So, calm down and we'll work through problems one or two at a time, I didn't want to blitz you with one giant super-post that fixed everything but left you none the wiser as to why ... ;)

Ok ... looking at your code, it seems you've fixed a couple of things I've said, but got rid of some other bits that were fine.

For instance, you've now made selc responsible for the Y position. This makes your code harder to understand. It's better to have selc tracking the selection number, and use a separate variable for the Y position. In fact, you don't need a variable for Y at all, you can calculate it from selc:

selectorPosition.Y = selc*40 + 140;



That way selc can start at zero, and increment with selc++, and check if(selc==0) for your menu options.

Also to save you some more code, you can test booleans with if(!upPressed) instead of if(upPressed==false), and if(upPressed) instead of if(upPressed==true). This is true of any boolean, including true/false returns from functions.

Ok onto the code... now you've changed it I can't work out why it's doing what you describe. I'd expect (from what I can see here) that your selector would move down ONCE and then never move again, except to move up once.

This is because when they first press down, you set "downPressed=true". So from this point onward, the if(downPressed==false) statement would never be true, and so your if(selc<200) will never be reached again.

From this I conclude that somewhere else you are resetting downPressed=false and upPressed=false, and this other place is getting called very frequently (for instance in your Draw method), so your selector keeps moving down. Can you post the rest of the program as well?

I've added some comments to your Update method to show you how the program flow is working. I've marked important bits with **asterisks. I've also mentioned a couple of other things which I noticed.


// ** The Update function is called LOTS of times per second, so you can update your game's state as often as you need **
protected override void Update(GameTime gameTime)
		{
			// Get the current state of the keyboard
			KeyboardState newState = Keyboard.GetState();
		
   
			if (newState.IsKeyDown(Keys.Up))
			{
				// We ONLY reach this line if the UP key is being pushed
				if (upPressed == false)
				{
					// We ONLY reach this line if upPressed is FALSE
					if (selc > 140)
					{
						selc = selc - 40;
						// **Here we set upPressed to TRUE
						upPressed = true;
					}
					else
					{
/* We never reach here because it is skipped the first time round (that's what "else" means) and by the second time round, upPressed has already been changed to TRUE.
	Incididentally, why do you set selc = 200? This means once you try to cursor up past the top element, your selector jumps to 200, which is 1 and a half positions down? Seems a bit random... */
						selc = 200;
						// The key is in this next line - you don't want it here, you want it somewhere else ...
						upPressed = false;
					}
				}
			} 
			else {
			  // Here's a clue: we'd only reach THIS line if ** the "up" key is NOT pressed **
			}

			// The same from the above is entirely true of this code:
			if (newState.IsKeyDown(Keys.Down))
			{
				if (downPressed == false)
				{
					if (selc < 200)
					{
						selc = selc + 40;
						downPressed = true;
					}
					else
					{
						selc = 150;
						downPressed = false;
					}
				}
			}
			base.Update(gameTime);
		}  


Hope that helps, but please post the entire program as well, I'm pretty there are problems elsewhere that will still stop things working ... You're nearly there though, trust me ;)

--serializer

This post has been edited by serializer: 16 August 2007 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1