HaxeFlixel / flixel

Free, cross-platform 2D game engine powered by Haxe and OpenFL
https://haxeflixel.com/
MIT License
1.99k stars 439 forks source link

FlxG.gamepads.getByID().pressed not working properly with multiple controllers #1892

Open nospoone opened 8 years ago

nospoone commented 8 years ago

Code snippet reproducing the issue:

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.math.FlxMath;

class MenuState extends FlxState
{

    private var gamepadList:FlxText;
    private var state:FlxText;

    private var activeID:Int;

    override public function create():Void
    {
        gamepadList = new FlxText(10, 10, 0, "", 16);
        add(gamepadList);

        state = new FlxText(10, 100, 0, "", 16);
        add(state);

        super.create();
    }

    override public function update(elapsed:Float):Void
    {       
        activeID = FlxG.gamepads.getFirstActiveGamepadID();

        gamepadList.text = "";
        state.text = "";

        for (gamepad in FlxG.gamepads.getActiveGamepads()) {
            gamepadList.text += gamepad.detectedModel + ' at ID #' + gamepad.id + '\n';
        }

        state.text += "Active Gamepads: " + FlxG.gamepads.numActiveGamepads;
        state.text += "\nFirst Active Gamepad ID: " + activeID;

        if (activeID > -1)
            state.text += "\nFlxG.gamepads.getByID(" + activeID + ").pressed.ANY returned " + FlxG.gamepads.getByID(activeID).pressed.ANY;

        super.update(elapsed);
    }
}

Observed behavior: Whenever you have more than one controller plugged in, at least two of them need to have a button pressed in order for the code above to return true. Pressing only one controller returns false, for that controller ID. Same goes for justPressed and justReleased. Note that FlxG.gamepads.anyInput() will behave as expected.

Expected behavior: The controller that is active and pressed to return true.


Here's an example project exhibiting the issue.

Gama11 commented 8 years ago

This seems to only be an issue with FLX_JOYSTICK_API - doesn't happen on OpenFL Next or Flash (where FLX_GAMEPAD_API).