FlxButton justPressed behavior



  • Why does FlxButton fire justPressed twice in update()? If I put the super.update(elapsed) before, it still does. justReleased fires fires once if after super.update, but both fire twice if before it. justPressed always fire twice.

    This happens with flash, html5, neko, android.

    	private var _button:FlxButton;
    	
    	override public function create():Void
    	{
    		_button = new FlxButton(100, 100, "TEST");
    		add(_button);
    		
    		super.create();
    	}
    	
    	override public function update(elapsed:Float):Void 
    	{
    		if (_button.justPressed)
    			trace("justPressed");
    			
    		if (_button.justReleased)
    			trace("justReleased");
    	
    		super.update(elapsed);
    	}
    


  • I actually don't know, but for a good guess I would say there is some inhertant behaviour going on between the two events and you may need to be more specific in your logic than you originally thought you would need to be. Try, though not for production rather as an experiment some boolean flag to try and distinguish between pressed and released, see what happens



  • I did that. This fixes the problem, but it seems like I shouldn't have to do that.

    		if (_button.justPressed && !_buttonDown)
    		{
    			_buttonDown = true;
    			trace("justPressed");
    		}
    			
    		if (_button.justReleased && _buttonDown)
    		{
    			_buttonDown = false;
    			trace("justReleased");
    		}
    


  • I agree, hopefully someone will reply with a more detailed solution.

    You could say pressed and not released or released and not pressed in the events though this is obviously the same.



  • Maybe nest the just released inside of the just pressed.

    I think that due to how fast delta time runs both events are being fired within the scope of the update method due to some sort of polymorphic behaviour.

    Try exploring

    For(pressed in pressedevents)
    {
    Pressed(){}
    JustReleased(){}
    }

    I've seen code like that in touch events for android



  • Since I'm in update(), pressed fires continuously. justReleased fires twice, too. Unless I put it after super.update. I'm using it for my Player movement and jumps, so justPressed is what it needs to be to allow short or long jumps. I also allow for two jumps, so it buggers that, too. I can certainly use a bool as a workaround, but it seems like a bug. I also tried using the onClick callback of the button, but that only gets the justReleased.



  • Yes. Something to do with the time elapsed in update(). I made a timer and moved the code to an updateClick() function. When the timer is set for 0.0167 (60FPS) it gives the same double-fire. If I set it to 0.0333 (30FPS) it fires once, and seems consistent. If I set it larger, like 0.05, it's inconsistent. I have a feeling the event is based on 30FPS, but I'm just guessing. At least it seems to work as expected at 0.0333.



  • Thanks for your input @neal. For now I'm either going to prevent the double-fire with a boolean check, or break the code out of update() to a new function and run a timer at 30FPS. The boolean check will probably be more reliable. If anyone knows of a better way to handle this, please let me know.

    Update: Definitely going with the boolean check. The timer may have worked on a simple example but not so good in the real world.


Log in to reply