Problems accessing a "public static var"

23 views
Skip to first unread message

JustGabe

unread,
Dec 5, 2015, 5:10:13 PM12/5/15
to HaxeFlixel
So basically here's the simplified code of the class:
class Car
{
    public static var noOfWheels:Int = 4;

public function new() 
{
}

    public static function getNoOfWheels():Int
    {       
        return Car.noOfWheels;
    }
}

But when I try to access the var "noOfWheels", in any of these possible methods, it throws me an error:
 trace (myCar.noOfWheels);
Cannot access static field noOfWheels from a class instance

 trace (myCar.getNoOfWheels());
Cannot access static field getNoOfWheels from a class instance

 trace (Type.getClass(myCar).noOfWheels);
Class<Car> has no field noOfWheels

So, how the hell am I supposed to get the value of the var from Main!?

Ashiq A.

unread,
Dec 5, 2015, 5:14:46 PM12/5/15
to haxef...@googlegroups.com
Static in Haxe is the same as in other languages. Instead of "myCar.noOfWheels", you should be trying to access Car.noOfWheels or Car.getNoOfWheels.

--
HaxeFlixel Development Community
See our github https://github.com/haxeflixel/ and our documentation http://haxeflixel.com/documentation/
---
You received this message because you are subscribed to the Google Groups "HaxeFlixel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haxeflixel+...@googlegroups.com.
Visit this group at http://groups.google.com/group/haxeflixel.
To view this discussion on the web visit https://groups.google.com/d/msgid/haxeflixel/7a16ae0b-9c75-46b0-b24e-1f409a3e288f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

JustGabe

unread,
Dec 5, 2015, 5:34:53 PM12/5/15
to HaxeFlixel
But that only solves half the problem, what if I make multiple Cars with different noOfWheels values? how do I access them?
for (i in 0...5)
{
myCar = new Car(i);
cars.push(myCar);
}

class Car
{
    public static var noOfWheels:Int;

public function new(num:Int) 
{
noOfWheels = num;
}
}

Ashiq A.

unread,
Dec 5, 2015, 5:53:52 PM12/5/15
to haxef...@googlegroups.com
I'm not sure what you're trying to accomplish by doing that.

Static variables are stored conceptually with a class.

In your case, all cars would get the same value of noOfWheels, which is the last assigned value, which (in your case) is 4 or 5 (not sure if haxe's "..." iterator is inclusive or exclusive of the value).

This is nothing special to Haxe; it works the same way in Java, C#, Ruby, PHP, etc.

Feel free to experiment here: http://try.haxe.org/

--
HaxeFlixel Development Community
See our github https://github.com/haxeflixel/ and our documentation http://haxeflixel.com/documentation/
---
You received this message because you are subscribed to the Google Groups "HaxeFlixel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haxeflixel+...@googlegroups.com.
Visit this group at http://groups.google.com/group/haxeflixel.

JustGabe

unread,
Dec 5, 2015, 6:11:37 PM12/5/15
to HaxeFlixel
I'm programming an RPG where every time the player enters a new room, a number of NPC's are loaded in the new map (if there's any), each one with their own array of dialogues, for example:
class Char extends FlxSprite
{
  public static var dialogue:Array<String> = [];

  public function new(dialogue:Array<String>) 
  {
    this.dialogue = dialogue;
    //more code...
  }
}
var chat:Array<Array<String>> = [
["line 00", "line 01"],
["line 02", "line 03"],
["line 04", "line 05", "line 06"]
];
for (i in 0...3)
{
char = new Char(chat[i]);
chars.push(char);
}

But if I do
trace(char[0].dialogue);
the "Cannot access static field dialogue from a class instance"

Dlean Jeans

unread,
Dec 6, 2015, 1:06:12 AM12/6/15
to HaxeFlixel
Maybe you shouldn't make the `dialogue` static.

About the `Car`, there are 2 ways:
1. You should make `noOfWheels` non-static
2. You ought to make 2 class of car like `FourWheel' and `FiveWheel` with static `noOfWheels`

Jeru Sanders

unread,
Dec 6, 2015, 3:06:47 PM12/6/15
to HaxeFlixel
That behaviour is the definition of static, when you call Math.random(), you don't have to make a "new Math()", because the function is static and instances don't matter.

You probably don't actually want a static here, what makes you think static is the keyword you need to use here?
Reply all
Reply to author
Forward
0 new messages