Search code examples
classhaxeopenfl

Importing local classes in Haxe


I'm starting my first serious project in Haxe, but I've hit somewhat of a snag.

The problem is that if I try writing a class in a file other than my Main.hx, it is not being compiled. I've tried using import but that does not seem to work.

I'm using vim as a my ide with a few nice plugins(Youcompleteme, syntastic and vaxe) and the only haxelib is openfl.

Here is the boilerplate being created when I use the command openfl create project :

Project.xml

<?xml version="1.0" encoding="utf-8"?>
<project>

    <meta title="ProjetSession" package="com.sample.projetsession" version="1.0.0" company="Company Name" />
    <app main="Main" path="Export" file="ProjetSession" />

    <source path="Source" />

    <haxelib name="openfl" />

    <assets path="Assets" rename="assets" exclude="openfl.svg" />
    <icon path="Assets/openfl.svg" />

</project>

ProjetSession.hxproj

<?xml version="1.0" encoding="utf-8"?>
<project version="2">
  <!-- Output SWF options -->
  <output>
    <movie outputType="CustomBuild" />
    <movie input="" />
    <movie path="project.xml" />
    <movie fps="30" />
    <movie width="800" />
    <movie height="600" />
    <movie version="1" />
    <movie minorVersion="0" />
    <movie platform="Lime" />
    <movie background="#FFFFFF" />
    <movie preferredSDK=";3;" />
  </output>
  <!-- Other classes to be compiled into your SWF -->
  <classpaths>
    <class path="D:\Development\Haxe\openfl" />
    <class path="d:\Development\Haxe\lime" />
    <class path="Source" />
    <class path="Export\html5\haxe" />
  </classpaths>
  <!-- Build options -->
  <build>
    <option directives="openfl=3.0.0-beta&#xA;lime=2.1.3&#xA;tools=2.1.3&#xA;no-compilation&#xA;openfl-html5&#xA;canvas&#xA;lime-html5&#xA;html5&#xA;web&#xA;html5" />
    <option flashStrict="False" />
    <option noInlineOnDebug="False" />
    <option mainClass="ApplicationMain" />
    <option enabledebug="False" />
    <option additional="--remap flash:openfl&#xA;--macro allowPackage(&quot;flash&quot;)" />
  </build>
  <!-- haxelib libraries -->
  <haxelib>
    <!-- example: <library name="..." /> -->
  </haxelib>
  <!-- Class files to compile (other referenced classes will automatically be included) -->
  <compileTargets>
    <!-- example: <compile path="..." /> -->
  </compileTargets>
  <!-- Paths to exclude from the Project Explorer tree -->
  <hiddenPaths>
    <hidden path="obj" />
  </hiddenPaths>
  <!-- Executed before build -->
  <preBuildCommand>"$(CompilerPath)/haxelib" run lime build "$(OutputFile)" $(TargetBuild) -$(BuildConfig) -Dfdb</preBuildCommand>
  <!-- Executed after build -->
  <postBuildCommand alwaysRun="False" />
  <!-- Other project options -->
  <options>
    <option showHiddenPaths="False" />
    <option testMovie="Custom" />
    <option testMovieCommand="" />
  </options>
  <!-- Plugin storage -->
  <storage />
</project>

The hml file is automatically generated by vaxe.


Solution

  • Defining a class and importing it, it is not sufficient to have that class compiled in your output. What you need is to actually "use" it. That can be accomplished by referring your class in your main function. To do that just use the class directly or use any other code that refer that class directly or indirectly. This is a big advantage of the language that doesn't overload the output of your app with unneeded code. That also means that you can use third party libraries with confidence, knowing that only the bits of library you use will be embedded in your output.