Hello Guest

Author Topic: Getting Controls to Register  (Read 3214 times)

0 Members and 1 Guest are viewing this topic.

holymoo

  • Member
  • Posts: 6
    • View Profile
Getting Controls to Register
« on: July 11, 2015, 06:37:38 PM »
I'm having issues getting the Pong tutorial to work. For whatever reason, I'm having issues getting it to register a key click.

Here is the setup of the controls:

Code: [Select]

            Global.PlayerOne = game.AddSession("P1");
            Global.PlayerTwo = game.AddSession("P2");
           
            Global.PlayerOne.Controller.AddButton("Up", "Down");
            Global.PlayerTwo.Controller.AddButton("Up", "Down");

            Global.PlayerOne.Controller.Button("Up").AddKey(Key.W);
            Global.PlayerOne.Controller.Button("Down").AddKey(Key.S);

            Global.PlayerTwo.Controller.Button("Up").AddKey(Key.Up);
            Global.PlayerTwo.Controller.Button("Down").AddKey(Key.Down);


Here is the update code to handle the keypresses

Code: [Select]

public override void Update()
        {
            base.Update();

            if (_player.Controller.Button(Tokens.Up).Down)
            {
                Y -= 5;
            }
            else if (_player.Controller.Button(Tokens.Down).Down)
            {
                Y += 5;
            }
        }


I suspect that there may be a race condition that is causing a problem, because when I set up a breakpoint on this method, it causes the key to be permanently registered in the down position.

Code: [Select]

        void OnKeyPressed(object sender, KeyEventArgs e) {
            if (Game.Debugger != null) {
                // Ignore presses from the debug toggle key.
                if ((Key)e.Code == Game.Debugger.ToggleKey) return;
            }

            if (!activeKeys[(Key)e.Code]) {
                keysPressed++;
            }
            activeKeys[(Key)e.Code] = true;
            LastKey = (Key)e.Code;
        }


Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Getting Controls to Register
« Reply #1 on: July 13, 2015, 05:03:14 AM »
Hey sorry the videos are super old now and I'm working on new examples.

I got the following code to work:
Code: [Select]
namespace inputBugTest {
    class Program {
        static void Main(string[] args) {
            var game = new Game();
            Global.PlayerOne = game.AddSession("P1");
            Global.PlayerOne.Controller.AddButton("up", "down");
            Global.PlayerOne.Controller.Button("up").AddKey(Key.Up);
            Global.PlayerOne.Controller.Button("down").AddKey(Key.Down);

            Global.PlayerTwo = game.AddSession("P2");
            Global.PlayerTwo.Controller.AddButton("up", "down");
            Global.PlayerTwo.Controller.Button("up").AddKey(Key.W);
            Global.PlayerTwo.Controller.Button("down").AddKey(Key.S);

            var scene = new Scene();
            scene.Add(new Player(100, 200, Global.PlayerOne));
            scene.Add(new Player(400, 200, Global.PlayerTwo));

            game.Start(scene);
        }
    }

    class Player : Entity {
        Session session;

        public Player(float x, float y, Session session) : base(x, y) {
            AddGraphic(Image.CreateRectangle(20));
            Graphic.CenterOrigin();
            this.session = session;
        }

        public override void Update() {
            base.Update();

            if (session.Controller.Button("up").Down) {
                Y -= 5;
            }
            if (session.Controller.Button("down").Down) {
                Y += 5;
            }
        }
    }
}

So I'm not entirely sure what is going on in your example yet.  Are you on the latest dev branch or default?

The breakpoint causing a permanent key down flag might be caused by the window losing focus when the key is released.  This is a common issue in games in general as keyboard input stops being processed when the window loses focus.  If the window never receives a key up event then the game thinks the key is still down until the window has focus while the key is pressed and released again.
« Last Edit: July 13, 2015, 05:08:51 AM by Kyle »