FlxBar wont update properly



  • Hey,

    Im working on a project where i'm using FlxBar to take track of stats(Health,Energy).
    When I change the value of the stats, the bar value changes but the Image of the bars stays the same.

    Anyone else had this problem and knows what to do?



  • I guess you have to call bar.updateBar()


  • administrators

    Actually, I think that should happen automatically.



  • @DleanJeans

    my game crashes if i use the update fuction the same counts for when i try to trace the Value of the bars. Invalid field access : get_value

    This is how I make my FlxBar:
    I have a class (MainState where you can see the stats)

    Public static var Health:Int = 100;
    
    
     override public function create():Void 
     {
        _barHealth  = new FlxBar(100, 520, LEFT_TO_RIGHT, 320, 66, null, "Health", 0, 100);
    	_barHealth.value = Health;
    	_barHealth.createImageBar(AssetPaths.HealthBarBackground__png, AssetPaths.HealthBar__png, null, null);
    	add(_barHealth);
     }
    

    and in the PlayState class i have a button with this function

             function  onClick():Void
     {
     MainState.Health = MainState.Health - 30;
     }		
    

    As you can see here on this image : Screenshot . The text on the bars do update but the bars itself not while they both are tracking the same Var (Health).



  • @Bas-Benjamins Changing MainState.Health doesn't change the value of _barHealth object.

    You should write "_barHealth.value = MainState.Health" too.

    We are making a MMORPG w/HaxeFlixel => Click Here to go to the Facebook Page

    0


  • Did that, but it didn't solve the problem. the bar stays the same.



  • You gotta pass the state to parentRef not null like this:

    _barHealth  = new FlxBar(100, 520, LEFT_TO_RIGHT, 320, 66, MainState, "Health", 0, 100);
    

    Also, nice graphic you've got there :D



  • Okay I Finally fixed the problem.

    The problem was that after I left the Mainstate where I show my Bars, the bars are all destroyed.

         FlxBar.hx:180: destroying: (min: 0 | max: 100 | range: 100 | %: 1 | px/%: 3.2 | value: 0)
    

    so when I tryed to update the bars there was none to update and it crashed. So what I'm doing now is updating the bars in MainState after I created the Bars again.

    public static var Health:Int = 0;
    
    override public function create():Void 
    {		
    	//Setup the HealthBar
    	_barHealth  = new FlxBar(100, 520, LEFT_TO_RIGHT, 320, 66, null, null, 0, 100);
    	_barHealth.value = 0;
    	_barHealth.createImageBar(AssetPaths.HealthBarBackground__png, AssetPaths.HealthBar__png, null, null);
    	add(_barHealth);
    
    	
    	updateBars(Health);
    }
    
    
    public function updateBars(amountHealth:Int ):Void
    {
    	_barHealth.value = amountHealth;			
    }	
    

    and in the onClick function just kept the same.
    (hope this is all clear what i'm telling:grimacing: )

    Thanks all for helping ^^ ,
    and Thanks @DleanJeans for the complement on the graphics ^^



  • Why update the bar manually but not let the bar update itself automatically by creating the bar this way:

      //_barHealth  = new FlxBar(100, 520, LEFT_TO_RIGHT, 320, 66, null, null, 0, 100);
        _barHealth  = new FlxBar(100, 520, LEFT_TO_RIGHT, 320, 66, MainState, "Health", 0, 100);
        _barHealth.createImageBar(AssetPaths.HealthBarBackground__png, AssetPaths.HealthBar__png, null, null);
        add(_barHealth);
    

    See the difference?


Log in to reply