ashes999
ashes999

Reputation: 10163

Haxe classes seem to use the filename, not the class name

I have a file called 1-DarknessScene.hx which contains class DarknessScene. When I try to reference this by creating a fully-qualified new com.foo.bar.scenes.DarknessScene(), I get a class not found error.

I double-checked the class/instance docs, but found no references to this behaviour.

Am I doing something wrong?

Upvotes: 0

Views: 225

Answers (1)

Jason O'Neil
Jason O'Neil

Reputation: 6008

The section in the manual you are looking for is this:

http://haxeorg.dev/manual/type-system-modules-and-paths.html

In Haxe, each ".hx" file is called a module, and it can contain one or more classes, typedefs, enums, interfaces etc. A few points:

  • Usually, the file name / module name is the same as that of the main class. So DarknessScene.hx, rather than 1-DarknessScene.hx.
  • This means your file names have the class naming rules apply, so they must begin with an upper-case letter, not a number. In your case if you want to have a number to sort the files, you might name you class "Scene01Darkness" or something similar.
  • The manual describes how you can have different types (classes, interfaces etc) inside a module, and reference them. A quick example:

Scenes.hx

package mygame;

class Scene01Darkness {
    // ...
}

And then import like this:

new mygame.Scenes.Scene01Darkness();

But the rules about class names (and I guess filenames) beginning with an upper case character still apply.

Good luck!

Upvotes: 1

Related Questions