XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

n00b qustion and menu problem

Last post 8/15/2007 2:53 PM by Pspfreak101. 5 replies.
  • 8/12/2007 3:56 PM

    n00b qustion and menu problem

    I started working on my menu and I have my game and menu in seprate scripts, anyways I need to run the game script from the menu  Now I'm having trouble with my menu when i add the loop it just freezes

    void Updatemenu(GameTime gameTime)
    {
    KeyboardState newState = Keyboard.GetState();

    while (newState.IsKeyDown(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 (selc == 0)
    {
    if (newState.IsKeyDown(Keys.Enter))
    {
    //Run Game here
    }
    }

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

    if (selc == 2)
    {
    if (newState.IsKeyDown(Keys.Enter))
    {
    this.Exit();
    }
    }
    base.Update(gameTime);
    }
    }
  • 8/12/2007 9:14 PM In reply to

    Re: n00b qustion and menu problem

    If you are calling Updatemenu between each frame, it may be going through several (more than 3) updates each time you press the key. If this is the case, then selc would increment to 3 or highter within 0.067 seconds (before releasing the key) & none of the if statements would be true.

    If you suspect this is happening, maybe keep track of the previous frames' keyboardstate and only allow the while statement to be true if the down key is pressed & it was in the released state in the previous frames' keyboardState.
    while(newState.IsKeyDown(Keys.Down) && lastState.IsKeyUp(Keys.Down))
    That way, when you press it, While will be true only for the first update & if you still have the key pressed the nextime update is called, while will be false & won't increment selc.

    Also, I don't know for sure how your logic flow shoud be, but it looks like it may help to move the while's ending curly brace up to just before the 2nd  if(selc == 0) line. This will allow the 2nd set of if statements & the base.Update to run even if the down key is not pressed if that were needed.

    Unlock The *I don't use 3 angles in a Vector3 for rotations anymore* Achievement Here
  • 8/12/2007 9:51 PM In reply to

    Re: n00b qustion and menu problem

    Thanks it doesn't freeze anymore but it doesn't move
  • 8/14/2007 8:21 PM In reply to

    Re: n00b qustion and menu problem

    I think the problem is in your while loop. To me it looks like your gettin yourself stuck in aloop. Your adding one to selc -selc++;- , then resetting it to zero again -. Also (selc ==0) is never true. Unless selc is initialized at -1. Think thats correct anyway (It's quite late). What I would just try is just a couple of if statements:

     

    public void UpdateMenu(GameTime gameTime - not needed)

    {

    if (keyBoard.IsKeyDown(Keys.Up) && selectMenuItem > 0)

    selectMenuItem -= 1;

    if (keyBoard.IsKeyDown(Keys.Down) && selectMenuItem < 2)

    selectMenuItem += 1;

     

    if (selectMenuItem == 0 && (keyBoard.IsKeyDown(Keys.Space)) && lastState.IsKeyUp(Keys.Space))

    {

    //TODO

    }

    if ((selectMenuItem == 1 && (keyBoard.IsKeyDown(Keys.Space)))

    {

    //TODO

    }

    lastState = keyBoard ;

    }//UpdateMenu()

     

    For me that seems like an easier way to handle a small menu. Nothing fancy and no complicated while statements to deal with.

    SacredChimp

    You get great programmers, good and rubbish programmers ... then theres me!! Although I do try!!
  • 8/15/2007 7:11 AM In reply to

    Re: n00b qustion and menu problem

    1) If you do this:
    KeyboardState newState = Keyboard.GetState();

    while (newState.IsKeyDown(Keys.Down)) {
    /* ... */
    }

    you will query the keyboard state only once (in the first line) and then keep checking the saved state, which never changes. Thus the endless loop.

    2) You've got several lines like if(selc == 0) { /* ... */ selc = 0; }. Setting selc to 0 only when it is 0... doesn't really make sense :)

    3) Also, you're checking for the enter key inside your while loop, which is only entered when the down key is pressed. Then means you can only select a menu item if you press enter while you're also pressing down. In addition, as described in (1), you're only checking the keyboard state once at the top, so the user would have to press enter and down at exactly the same time, which is next to impossible.

    You should remove the while loop entirely, otherwise the game will stop drawing because it's looping with 100% CPU usage in that loop waiting for you to release the down key.
    Instead
    • Just check whether a key is down once per frame
    • If it is down, set a flag to remember it is down
    • Only check again when the key has been released in the meantime
    In code:
    void Updatemenu(GameTime gameTime) {

    KeyboardState newState = Keyboard.GetState();

    if(newState.IsKeyDown(Keys.Down)) {

    if(!this.downPressed) {
    ++this.selc;
    this.downPressed = true;
    }

    } else { // down is NOT pressed

    this.downPressed = false;

    }

    if(newState.IsKeyDown(Keys.Enter)) {
    switch(this.selc) {
    case 0: { /* code for first menu item */ break; }
    case 0: { /* code for second menu item */ break; }
    /* ... */
    }
    }
    }

    -Markus-

    Check out my website and blog for some interesting articles and useful utility classes!
    Nuclex Framework: threaded particles, skinnable GUI, vector fonts, texture atlasses and lots more.
    WiX XNA Installer: Professional-looking MSI installer template for XNA games.
  • 8/15/2007 2:53 PM In reply to

    Re: n00b qustion and menu problem

    thanks for the help

Page 1 of 1 (6 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG