Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2012
    Location
    Florida
    Posts
    8

    Unhappy Side Scrolling Beat'em up Help Needed

    Hello, I'm new and have started the "XNA side Scrolling Beat'em Up Tutorial" . I understand the concepts and I understand how to read and implement the code. The problem is that even though I have gotten Phase 1 game Skeleton and menu Implementation to compile it won't take input. I think that my problem is in the input helper class. But the code I've type is exactly what is in the tutorial.

    Here is my code for the input helper class:


    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Graphics;

    namespace SideScrollingBeatEmUp
    {
    static class InputHelper
    {
    private const int numberOfPlayers = 4;

    public static GamePadState[] OGS = new GamePadState[numberOfPlayers];
    public static GamePadState[] NGS = new GamePadState[numberOfPlayers];
    public static KeyboardState OKS;
    public static KeyboardState NKS;


    public static void UpdateStates()
    {
    //Update GamePads
    for (int i = 0; i < numberOfPlayers; i++)
    {
    OGS[i] = NGS[i];
    NGS[i] = GamePad.GetState((PlayerIndex)i);
    }
    //update key boardstate
    OKS = NKS;
    NKS = Keyboard.GetState();
    }

    public static bool WasButtonPressed(PlayerIndex
    playerIndex,Buttons button)
    {
    return NGS[(int)playerIndex].IsButtonDown(button) && OGS[(int)
    playerIndex].IsButtonUp(button);

    }

    public static bool WasKeyPressed(Keys key)
    {
    return NKS.IsKeyDown(key) && OKS.IsKeyUp(key);
    }

    public static bool IsKeyHeld(Keys key)
    {
    return NKS.IsKeyDown(key);
    }



    }




    }
    for some reason When I hit space or what ever key I've designated to got to the main menu it ignores my key press. I'm using Microsoft Visual C# 2008 with XNA 3.1. All help would be greatly appreciated. The material is so clear at last and I'm not gonna be stopped before I get started. Thanks in advance.

    P.S.
    Menu Manager may have some issues as well. here's a look

    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Graphics;

    namespace SideScrollingBeatEmUp
    {
    enum MenuState
    {
    TitleScreen,
    MainMenu
    }
    struct MenuItem
    {
    string text;
    Vector2 position;
    public MenuItem(string text, Vector2 position)
    {
    this.text = text;
    this.position = position;
    }

    public void Draw(SpriteBatch SBHUD, Color drawColor)
    {
    SBHUD.DrawString(Game1.FontSmall, text, position, drawColor);
    }
    }

    static class MenuManager
    {
    static Color ColorStandard = Color.White;
    static Color ColorSelected = Color.LightBlue;

    static List<MenuItem>menuItems = new List<MenuItem>();
    static int currentMenuItem = 0;
    public static MenuState MenuState = MenuState.TitleScreen;

    public static void Update()
    {
    switch (MenuState)
    {
    case MenuState.TitleScreen:

    if (InputHelper.WasButtonPressed(PlayerIndex.One, Buttons.A)
    || InputHelper.WasButtonPressed(PlayerIndex.One, Buttons.Start)
    || InputHelper.WasKeyPressed(Keys.Space))
    {
    MenuState = MenuState.MainMenu;
    }
    break;

    case MenuState.MainMenu:


    if (InputHelper.WasButtonPressed(PlayerIndex.One, Buttons.DPadUp)
    || InputHelper.NGS[(int)PlayerIndex.One].ThumbSticks.Left.Y <
    0.3 && InputHelper.OGS[(int)PlayerIndex.One].ThumbSticks.Left.Y > 0.3
    || InputHelper.WasKeyPressed(Keys.Up))
    {
    currentMenuItem--;
    if (currentMenuItem < 0)
    currentMenuItem = menuItems.Count - 1;

    }

    if (InputHelper.WasButtonPressed(PlayerIndex.One, Buttons.DPadDown)
    || (InputHelper.NGS[(int)PlayerIndex.One].ThumbSticks.Left.Y <
    -0.3 &&
    InputHelper.OGS[(int)PlayerIndex.One].ThumbSticks.Left.Y >
    -0.3)
    || InputHelper.WasKeyPressed(Keys.Down))
    {
    currentMenuItem++;
    if (currentMenuItem >= menuItems.Count)
    currentMenuItem = 0;

    }
    // Menu Item actions
    if (InputHelper.WasButtonPressed(PlayerIndex.One, Buttons.A)
    || InputHelper.WasKeyPressed(Keys.Space))
    {
    switch (currentMenuItem)
    {
    case 0:
    GameManager.GameState = GameState.Playing;
    break;
    case 1:
    GameManager.GameState = GameState.HowToPlay;
    break;
    case 2:
    Game1.ExitGame();
    break;
    }




    }

    break;
    }


    }
    public static void Draw( SpriteBatch SBHUD)
    {
    switch (MenuState)
    {
    case MenuState.TitleScreen:
    SBHUD.DrawString(Game1.FontLarge, "Title Screen", new Vector2(187, 100), Color.White);
    SBHUD.DrawString(Game1.FontLarge, "Press Start", new Vector2(200, 300), Color.White);

    break;

    case MenuState.MainMenu:
    for (int i = 0; i < menuItems.Count; i++)
    {
    if (i == currentMenuItem)
    menuItems[i].Draw(SBHUD, ColorSelected);
    else
    menuItems[i].Draw(SBHUD, ColorStandard);
    }

    break;
    }
    }

    public static void CreateMenuItems()
    {
    menuItems.Add(new MenuItem("Begin Game", new Vector2(45, 50)));
    menuItems.Add(new MenuItem("How To Play", new Vector2(45, 120)));
    menuItems.Add(new MenuItem("Exit Game", new Vector2(45, 190)));

    }


    }


    }
    I really hope this isn't a development environment issue Peace.

  2. #2
    Join Date
    May 2009
    Posts
    199
    Hi Spencer,

    I'm not familiar with this particular tutorial but will help if I can.

    First things first, are any of the key/button presses working? Is it only the Space Bar keypress that is not recognised?

    I can see nowhere in the code above where you call the InputHelper.UpdateStates() method.
    You may already be calling it elsewhere in your code but if not it should probably be the first thing called in the Update() method of your main game class.

    If the above is not the problem, have you tried placing a few breakpoints in your code and seeing what values are contained in the relevant objects, such as the NKS and OKS?

    Also, a breakpoint inside the first if statement in the Update() method of the MenuManager class would at least let you know if pressing the space bar results in that if statement returning true.

  3. #3
    Join Date
    Jun 2012
    Location
    Florida
    Posts
    8
    Thank you for responding. Yes it is not recognizing any key presses. I will implement the fix you suggested asap!
    Last edited by SpencerMVC3; 02-20-2013 at 03:59 AM.

  4. #4
    Join Date
    May 2009
    Posts
    199
    Cool, let us know if it works.

  5. #5
    Join Date
    Jun 2012
    Location
    Florida
    Posts
    8
    That was it thanks a million

  6. #6
    Join Date
    May 2009
    Posts
    199
    No problem mate

  7. #7
    Join Date
    Jun 2012
    Location
    Florida
    Posts
    8
    Hi every one its me again. I'm not gonna lie this may be a war of attrition. I've been having laptop issues so its been a while. anyway. Here is my current stumbling block

    error message:
    Object reference not set to an instance of an object.


    here is the code for the level class


    Code:
    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Graphics;
    
    namespace SideScrollingBeatEmUp
    {
        class Level
        {
            struct BackgroundItem
            {
                Texture2D texture;
                Vector2 position;
                float speed;
                float layerDepth;
    
                public BackgroundItem(Texture2D texture, Vector2 position, float speed, float layerDepth)
                {
                    this.texture = texture;
                    this.position = position;
                    this.speed = speed;
                    this.layerDepth = layerDepth;
                }
    
                public void Draw(SpriteBatch SB)
                {
                    SB.Draw(this.texture, Camera.GetScreenPosition(this.position), null, Color.White, 0f,
                        Vector2.Zero, 1f, SpriteEffects.None, this.layerDepth);
                }
    
    
    
    
            }
            List<BackgroundItem> backgrounds;
            public List<Actor> Actors;
            public Rectangle Playbounds;
            public Player Player1;
    
            public Level()
            {
                backgrounds = new List<BackgroundItem>();
                Actors = new List<Actor>();
    
            }
    
            public void Update(GameTime gT)
            {
                // Update Actors
                for (int i = 0; i < Actors.Count; i++)
                {
                    Actors[i].Update(gT);
                }
                //Update Camera Positions
                UpdateCameraPosition();
                
                //Extend the Play Area
                if (InputHelper.WasKeyPressed(Keys.Q))
                    AddToPlayArea(150);
    
    
    
            }
    
            public void Draw(SpriteBatch SB, SpriteBatch SBHUD)
           {
             //Draw Backgrounds
               for (int i = 0; i < backgrounds.Count; i++)
               {
                   backgrounds[i].Draw(SB);
               }
    
                // Draw Actors
                for (int i =0; i < Actors.Count; i ++ )
                {
                    Actors[i].Draw(SB);
                }
    
           }
            private void UpdateCameraPosition()
            {
                if (Player1.Position.X > Camera.Position.X) <------------------ Error message highlights this line 
                {
                    Camera.Position.X += 3;
                    if (Camera.Position.X + Game1.SCREEN_WIDTH / 2 > this.Playbounds.Right)
                    {
                        Camera.Position.X = this.Playbounds.Right - Game1.SCREEN_WIDTH / 2; 
                    }
                }
    
            }
            public void AddBackgroundItem(Texture2D texture, Vector2 position, float speed, float layerDepth)
            {
                backgrounds.Add(new BackgroundItem(texture, position,speed, layerDepth));
            }
    
            public void AddToPlayArea(int howMuch)
            {
                this.Playbounds.Width += howMuch;
            }
    
        }
    }
    here is the camera class code


    Code:
    using System;
    using Microsoft.Xna.Framework;
    
    
    namespace SideScrollingBeatEmUp
    {
        class Camera
        {
            public static Vector2 Position;
    
            public static Vector2 GetScreenPosition(Vector2 worldPosition)
            {
                return worldPosition - Camera.Position +
                    new Vector2(Game1.SCREEN_WIDTH / 2, Game1.SCREEN_HEIGHT /2);
            }
        }
    }
    once again thanks in advance.
    Last edited by SpencerMVC3; 04-12-2013 at 05:44 PM.

  8. #8
    Join Date
    Jun 2012
    Location
    probably at a PC
    Posts
    309
    When this problem stops the program and points you to the mentioned line, you can hover your mouse over 'Player1' and see if it holds any value or if it's 'null' ?
    I haven't looked through everything, but from the looks of it, all you have is a variable 'Player1' of type Player, but it never get's handed value?!

    Also, is there a reason why your 'camera' class is not static and everything in it is?


    PS: I think it would be greatly appreciated by many if you use [ CODE ]-blocks[ /CODE ] for your code. It makes it so much easier to read IMHO.
    Last edited by nGAGE; 04-11-2013 at 07:54 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •