FlxG.gamepads.anyJustMovedXAxis ?

30 views
Skip to first unread message

Ats

unread,
Feb 12, 2015, 4:08:40 AM2/12/15
to haxef...@googlegroups.com
Hi, I'm still struggling ith the gamepads... I'm trying to get each and every gamepads to do the same thing.
For pressing buttons, I'm using FlxG.gamepads.anyJustPressed(buttonID). But for the joystick movement, I'm clueless...

Here's what I tried to do:


private var gamepad:FlxGamepad;

override public function create():Void
{
 super.create();
 FlxG.gamepads.globalDeadZone = Util.DEADZONE;
}

override
public function update():Void
{
 super.update();
 gamepad
= FlxG.gamepads.lastActive;
 if (gamepad != null) {
  var newAxis:Int = getAxis(gamepad);
    switch (newAxis) {
   case 1: // DOWN-LEFT, do thing....
   case 2: // DOWN, and so on...
  }
 
}
}

// Retrieve Gamepad Axis as an Int corresponding to the numeric pad directions
private function getAxis(gamepad:FlxGamepad):Int
{
 
var xAxis:Float = gamepad.getXAxis(0);
 
var yAxis:Float = gamepad.getYAxis(1);
 
if (xAxis < 0 && yAxis > 0) return 1; // DOWN-LEFT
 
else if (xAxis == 0 && yAxis > 0) return 2; // DOWN
 
else if (xAxis > 0 && yAxis > 0) return 3; // DOWN-RIGHT
 
else if (xAxis < 0 && yAxis == 0) return 4; // LEFT
 
else if (xAxis > 0 && yAxis == 0) return 6; // RIGHT
 
else if (xAxis < 0 && yAxis < 0) return 7; // UP-LEFT
 
else if (xAxis == 0 && yAxis < 0) return 8; // UP
 
else if (xAxis > 0 && yAxis < 0) return 9; // UP-RIGHT
 
else return 5; // CENTER
}

Problem is, I only get the last gamepad axis input, even if it is 0, 0.
I think the problem comes from gamepad = FlxG.gamepads.lastActive;
I tried gamepad = FlxG.gamepads.getFirstActiveGamepad(); but the result is worse. It only moves from time to time...

So why isn't there a FlxG.gamepads.anyJustMovedXAxis() ? or something like that? What did I miss?
Thanks for your help :)

Ats

unread,
Feb 12, 2015, 7:18:45 AM2/12/15
to
OK, so I've obtained what I wanted by adding two functions in FlxGamepadManager.hx :

 public function anyMoveXAxis(AxisID:Int):Float
 
{
   
for (gamepad in _gamepads)
   
{
          var axisValue:Float = gamepad.getXAxis(AxisID);

     if ((gamepad != null) && axisValue != 0)
     {
       
return axisValue;
     
}
   
}
 
 
return 0;
 
}
 
 
public function anyMoveYAxis(AxisID:Int):Float
 
{
   for (gamepad in _gamepads)
   
{
          var axisValue:Float = gamepad.getYAxis(AxisID);

     if ((gamepad != null) && axisValue != 0)
     {
       return axisValue;
     
}
   
}
 
 
return 0;
 
}


And by calling them using

public static inline function getAxis():Int
{
 
var xAxis:Float = FlxG.gamepads.anyMoveXAxis(LEFT_ANALOGUE_X);
 
var yAxis:Float = FlxG.gamepads.anyMoveYAxis(LEFT_ANALOGUE_Y);



 
if (xAxis < 0 && yAxis > 0) return 1; // DOWN-LEFT
 
else if (xAxis == 0 && yAxis > 0) return 2; // DOWN
 
else if (xAxis > 0 && yAxis > 0) return 3; // DOWN-RIGHT
 
else if (xAxis < 0 && yAxis == 0) return 4; // LEFT
 
else if (xAxis > 0 && yAxis == 0) return 6; // RIGHT
 
else if (xAxis < 0 && yAxis < 0) return 7; // UP-LEFT
 
else if (xAxis == 0 && yAxis < 0) return 8; // UP
 
else if (xAxis > 0 && yAxis < 0) return 9; // UP-RIGHT
 
else return 5; // CENTER
}

Do you think it is possible to add those two functions into haxeflixel sourcecode? Or is there a simple way to do the same without that?
Thanks

Ats

unread,
Feb 12, 2015, 1:06:13 PM2/12/15
to haxef...@googlegroups.com
I had to call gamepad.getXAxis(AxisID) twice (and same for getYAxis because on Android, what I wrote previously was crashing the game.
So here's my functions added in FlxGamepadManager.hx with the description:

 /**
 * Check to see if the X axis is moved on any Gamepad.
 *
 * @param AxisID The axis id
 * @return Float Value from -1 to 1
 */

 
public function anyMoveXAxis(AxisID:Int):Float
 
{
 
for (gamepad in _gamepads)
 
{

 
if ((gamepad != null) && gamepad.getXAxis(AxisID) != 0)
 
{
 
return gamepad.getXAxis(AxisID);
 
}
 
}
 
 
return 0;
 
}
 
 
/**
 * Check to see if the Y axis is moved on any Gamepad.
 *
 * @param AxisID The axis id
 * @return Float Value from -1 to 1
 */

 
public function anyMoveYAxis(AxisID:Int):Float
 
{
 
for (gamepad in _gamepads)
 
{

 
if ((gamepad != null) && gamepad.getYAxis(AxisID) != 0)
 
{
 
return gamepad.getYAxis(AxisID);
 
}
 
}
 
 
return 0;
 
}


Do you think it is worth adding those to the haxeflixel repo? (I don't know how git works...)


On Thursday, February 12, 2015 at 10:08:40 AM UTC+1, Ats wrote:
Reply all
Reply to author
Forward
0 new messages