public static var issues



  • This is sort of a follow up to a post I made a few months ago. My issue then was variables reset when I changed states and came back. The solution was to save all stats in a separate class (Stats.hx for example) as static variables.
    Well I came back to the project after a bit, and I'm running into some issues related to the static variables.
    Basically my code goes: press a button and it increases a public static variable in the Stats.hx class. However when I test I get the error, "cannot access static field _animationSkill from a class instance". I'm not too experienced with working with static variables or anything like that, so help would be appreciated.

    Here is some code samples from the project:

    class Stats 
    {
    	public static var _animationSkill:Float = 3;
    	
    	public function new() 
    	{
    		
    	}
    	
    }
    
    

    simplified version of the button example:

    class PCState extends FlxState 
    {
    	private var _btnAnimate:FlxButton;
    	
    	private var Stats:Stats;
    	
    	override public function create():Void 
    	{
    		_btnAnimate = new FlxButton(20, 22, "Animate", clickAnimate);
    		add(_btnAnimate);
    		
    		super.create();
    	}
    	
    	override public function update(elapsed:Float):Void 
    	{
    		super.update(elapsed);
    	}
    	
    	private function clickAnimate():Void
    	{
    		Stats._animationSkill += 1;
    	}
    }
    

  • administrators

    Should be good if you simply remove the following from your PCState:

    private var Stats:Stats;
    

    Stats also doesn't need a constructor.



  • ahhh, that was the issue I knew it was gonna be something simple like that. Thanks Gama!


Log in to reply
 

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