我正在浏览Haxe Introduction,我希望我的源文件夹有条理。到目前为止,我只有一些继承示例放在一个文件夹中。
我的问题是我收到type not found
错误,不允许我运行我的代码。我究竟做错了什么?
我有一个像这样的文件夹:
在这个文件夹中我有:
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;
}
}
答案 0 :(得分:2)
在查看了很多其他人的代码后,我发现了我的错误。事实证明我需要在我的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;
...