Search code examples
haxehaxeflixel

Creating an organized source folder in HaxeFlixel


I'm running through Haxe Introduction and I wanted to get my source folder organized. So far I just have some inheritance examples to put in one folder.

My problem is I'm getting a type not found error that wont allow me to run my code. What am I doing wrong?

I have a folder structured like this:

  • source/inheritance

In this folder I have:

  • Animal.hx
  • Dog.hx
  • Cat.hx

Cat.hx

package;

// package inheritance;

class Cat extends Animal{
    public function new(name:String, speed:Float, height:Float, weight:Float, legs:Int):Void {
        super(name, speed, height, weight, legs);   
    }

    public override function Speak():String {
        return ("Meow");
    }
}      

Animal.hx

package inheritance;

class Animal {
    private var name:String;
    private var speed:Float;
    private var height:Float;
    private var weight:Float;
    private var legs:Int;

    private function new(name:String, speed:Float, height:Float, weight:Float, legs:Int):Void {
        this.name = name;
        this.speed = speed;
        this.height = height;
        this.weight = weight;
        this.legs = legs;
    }

    private function toString():String {
        return ("\nName: " + name +  "\nSpeed: " + speed + " mph\nHeight: " + height + " inches\nWeight: " + weight + " lbs\nLegs: " + legs);
    }

    private function Speak():String {
        return ("Needs to fill out");
    }
}

Main.hx

package;
// package inheritance; //Tried both and neither work
// import inheritance; //Tried both and neither work

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.math.FlxMath;

class MenuState extends FlxState
{
    private var x:Int;
    private var myPoint:Point;
    private var myPoint3D:Point3;
    private var myDog:Dog;
    private var myCat:Cat;

    override public function create():Void
    {
        myPoint  = new Point(7,0);
        myPoint3D = new Point3(7,6,0);
        myDog = new Dog("Dog", 10, 12, 15, 4);
        myCat = new Cat("Cat", 12, 7, 6, 4);
        trace(myCat);
        trace(myCat.Speak());
        super.create();
    }

    override public function update(elapsed:Float):Void
    {
        super.update(elapsed);
    }

    private function changeMe(?x:Int) {
        this.x = x;
    }
}

Solution

  • After looking through a lot of other peoples code I have found my error. It turns out I need to import the class in my Main.hx.

    Main.hx

    package;
    import inheritance.*; // This is what was missing
    import inheritance.Dog; //Can be used if you only want one file
    import inheritance.Cat; //Can be used if you only want one file
    
    class MenuState extends FlxState
    {
        private var x:Int;
        private var myPoint:Point;
        private var myPoint3D:Point3;
        private var myDog:Dog;
        private var myCat:Cat;
    ...