FlxButton justPressed/justReleased fires twice



  • When you justPressed or justReleased a button, it has 2 consecutive events.
    Does anyone know why this happens? I have this workaround, but is there any better way to deal with it?

    EDIT: If you move super.update(elapsed); to the top of update(), justReleased only fires once, but justPressed still fires twice.

    package;
    
    import flixel.ui.FlxButton;
    import flixel.FlxState;
    
    class TestState extends FlxState
    {
    	private var _button1:FlxButton;
    	private var _button2:FlxButton;
    	private var _buttonDown:Bool;
    	
    	override public function create():Void
    	{
    		_button1 = new FlxButton(100, 100, "NORMAL");
    		_button2 = new FlxButton(100, 130, "HACKED");
    		
    		add(_button1);
    		add(_button2);
    		
    		super.create();
    	}
    	
    	override public function update(elapsed:Float):Void 
    	{
    		if (_button1.justPressed)
    			trace("NORMAL justPressed"); // prints twice
    		
    		if (_button1.justReleased)
    			trace("NORMAL justReleased"); // prints twice
    		
    		if (_button2.justPressed && !_buttonDown)
    		{
    			_buttonDown = true;
    			trace("HACKED justPressed"); // prints once
    		}
    		
    		if (_button2.justReleased && _buttonDown)
    		{
    			_buttonDown = false;
    			trace("HACKED justReleased"); // prints once
    		}
    		
    		super.update(elapsed);
    	}
    }
    


  • I've searched for this issue and there's nothing I can find about it. I can't believe that this isn't a problem for others. I'm targeting all platforms so I need a cross-platform solution. I'm using a virtualPad for mobile and keys for desktop. I've created an IC (InputControl) class that handles both types of input and I plan to add gamepad later. It works great except for the double fire of button click events. With it I can use IC.left.justPressed, IC.x.justReleased, IC.shoot.pressed etc., and it works for keys or buttons depending on which is used. If buttons justPressed or justReleased fire two events, it's not of much use since I only want one to do one thing, once. When using keys, it works as expected with the single event firing.

    My example above shows the problem. I hope someone can shed some light on this.

    Thanks for any help or insight.



  • hi dean. this code works.

    package;
    
    import flixel.ui.FlxButton;
    import flixel.FlxState;
    
    class PlayState extends FlxState
    {
    	private var _button1:FlxButton;
    	private var _button2:FlxButton;
    	private var _buttonDown:Bool;
    	
    	override public function create():Void
    	{
    		_button1 = new FlxButton(100, 100, "NORMAL");
    		_button2 = new FlxButton(100, 130, "HACKED");
    		
    		add(_button1);
    		add(_button2);
    		
    		super.create();
    	}
    	
    	override public function update(elapsed:Float):Void 
    	{
    		
    		
    		if (_button1.status == FlxButton.PRESSED)
    		{
    			if (_button1.justPressed)
    				trace("NORMAL justPressed"); // prints twice
    		}
    		
    		if (_button1.status == FlxButton.NORMAL)
    		{
    			if (_button1.justReleased)
    				trace("NORMAL justReleased"); // prints twice
    		}
    		
    		if (_button2.justPressed && !_buttonDown)
    		{
    			_buttonDown = true;
    			trace("HACKED justPressed"); // prints once
    		}
    		
    		if (_button2.justReleased && _buttonDown)
    		{
    			_buttonDown = false;
    			trace("HACKED justReleased"); // prints once
    		}
    		
    		_button1.status = FlxButton.NORMAL;
    		super.update(elapsed);
    	}
    }


  • thanks @galoyo

    I'll try this way for my workaround since I wont need any bool flags. I still would like to fix the button itself, but I don't know how.

    EDIT: Checking and setting the button status isn't a good workaround. If you move off the button while holding down it misses the event on release or the next click. For now I'll use the bool flags. Thanks for trying, though.


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.