在java程序中“找不到符号”

时间:2011-04-20 17:06:29

标签: java classpath javac

所以这就是困境: 我正在使用两个不同的包使用两个不同的类编写程序。然而,当我尝试从BaseGame(带有方法的类)调用方法时。我检查了拼写,我只是感到困惑。

以下是代码:

来自Automobile.java:

package mygame.model;

import com.jme3.bullet.BulletAppState;
import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.MeshCollisionShape;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.bullet.nodes.PhysicsNode;
import com.jme3.bullet.objects.VehicleWheel;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Matrix3f;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.shadow.BasicShadowRenderer;
import com.jme3.texture.Texture.WrapMode;
import mygame.BaseGame;

public class Automobile extends Model {
    float stiffness;
    float compValue;
    float dampValue;
    float mass;
    Node node;
    VehicleControl vehicle;
    float wheelRadius;

    // <editor-fold defaultstate="collapsed" desc="findGeom">
private Geometry findGeom(Spatial spatial, String name) {
    if (spatial instanceof Node) {
        Node node = (Node) spatial;
        for (int i = 0; i < node.getQuantity(); i++) {
            Spatial child = node.getChild(i);
            Geometry result = findGeom(child, name);
            if (result != null) {
                return result;
            }
        }
    } else if (spatial instanceof Geometry) {
        if (spatial.getName().startsWith(name)) {
            return (Geometry) spatial;
        }
    }
    return null;
}// </editor-fold>
    private void buildVehicle() {
    //Load model and get chassis Geometry
    node = (Node)loadModel("Models/Ferrari/Car.scene");
    node.setShadowMode(ShadowMode.Cast);
    Geometry chasis = findGeom(node, "Car");
    BoundingBox box = (BoundingBox) chasis.getModelBound();

    //Create a hull collision shape for the chassis
    CollisionShape carHull = CollisionShapeFactory.createDynamicMeshShape(chasis);

    //Create a vehicle control
    vehicle = new VehicleControl(carHull, mass);
    node.addControl(vehicle);

    //Setting default values for wheels
    vehicle.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
    vehicle.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
    vehicle.setSuspensionStiffness(stiffness);
    vehicle.setMaxSuspensionForce(10000);

    //Create four wheels and add them at their locations
    //note that our fancy car actually goes backwards..
    Vector3f wheelDirection = new Vector3f(0, 1, 0);
    Vector3f wheelAxle = new Vector3f(-1, 0, 0);

    Geometry wheel_fr = findGeom(node, "WheelFrontRight");
    wheel_fr.center();
    box = (BoundingBox) wheel_fr.getModelBound();
    wheelRadius = box.getYExtent();
    float back_wheel_h = (wheelRadius * 1.7f) - 1f;
    float front_wheel_h = (wheelRadius * 1.9f) - 1f;
    vehicle.addWheel(wheel_fr.getParent(), box.getCenter().add(0, -front_wheel_h, 0),
            wheelDirection, wheelAxle, 0.2f, wheelRadius, true);

    Geometry wheel_fl = findGeom(node, "WheelFrontLeft");
    wheel_fl.center();
    box = (BoundingBox) wheel_fl.getModelBound();
    vehicle.addWheel(wheel_fl.getParent(), box.getCenter().add(0, -front_wheel_h, 0),
            wheelDirection, wheelAxle, 0.2f, wheelRadius, true);

    Geometry wheel_br = findGeom(node, "WheelBackRight");
    wheel_br.center();
    box = (BoundingBox) wheel_br.getModelBound();
    vehicle.addWheel(wheel_br.getParent(), box.getCenter().add(0, -back_wheel_h, 0),
            wheelDirection, wheelAxle, 0.2f, wheelRadius, false);

    Geometry wheel_bl = findGeom(node, "WheelBackLeft");
    wheel_bl.center();
    box = (BoundingBox) wheel_bl.getModelBound();
    vehicle.addWheel(wheel_bl.getParent(), box.getCenter().add(0, -back_wheel_h, 0),
            wheelDirection, wheelAxle, 0.2f, wheelRadius, false);

    vehicle.getWheel(2).setFrictionSlip(4);
    vehicle.getWheel(3).setFrictionSlip(4);

}
public Automobile(Spatial sp){
    super(sp);
}
}

来自BaseGame.java

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.Savable;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import java.io.IOException;

public class BaseGame extends SimpleApplication{

public static void main(String[] args) {
    BaseGame app = new BaseGame();
    app.start();
}
public Spatial loadModel(String asset){
    return assetManager.loadModel(asset);
}
@Override
public void simpleInitApp() {

}

@Override
public void simpleUpdate(float tpf) {
    //TODO: add update code
}

@Override
public void simpleRender(RenderManager rm) {
    //TODO: add render code
}

}

*编辑 这是错误:

E:\Game\BasicGame\src\mygame\model\Automobile.java:64: cannot find symbol
symbol  : method loadModel(java.lang.String)
location: class mygame.model.Automobile
    node = (Node)loadModel("Models/Ferrari/Car.scene");

而且,我确实导入了这个课程。

1 个答案:

答案 0 :(得分:2)

loadModel是一种非静态方法。您需要从类的 object 实例中调用它。

此外,即使它是静态方法,您也应该从其类中调用它,例如BaseGame.loadModel("somestring");

在您编辑答案后,我认为这显然是问题所在:

  • 您在main(BaseGame)中实例化BaseGame app = new BaseGame();对象,但您似乎没有为它实现任何特殊内容。
  • 您在单独的包中有一个单独的类,它没有引用BaseGame实例,因为没有人通过这样的引用,import 实例化。