在Java程序中找不到符号

问题描述:

因此,这里的困境: 我正在编写一个程序,使用两个不同的类,使用两个不同的包。然而,当我尝试从BaseGame调用一个方法(该方法与类)。我检查了拼写,我只是困惑。在Java程序中找不到符号

下面的代码:

从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"); 

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

+3

什么是无法编译的代码行和消息是什么? – 2011-04-20 17:09:01

+0

你能粘贴整个代码(不只是片段),还有整个错误信息(请逐字逐句)? – 2011-04-20 17:09:05

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

而且,即使它是一个静态方法,你应该从它的类调用它,例如BaseGame.loadModel("somestring");

后您编辑你的答案,我认为这是明确的问题:

  • 你实例化一个BaseGame对象,主要(BaseGame app = new BaseGame();),但你似乎没有实现任何特殊的东西。
  • 你有一个单独的类,在一个单独的包中,它没有提到BaseGame实例,因为没有人通过这样的引用,并且import而不是的一个实例。
+0

好吧,但是如果'Automobile'是'BaseGame'的一个子类出于某种奇怪的原因呢? ;-)或者,也许'Automobile'有它自己的'loadModel'方法来调用'base.loadModel(...)'? :-P(相当多为什么我要求整个代码文件,而不仅仅是片段。) – 2011-04-20 17:11:11

+0

@Chris - 你的评论是合理的,但由于OP没有提及这样的事情和名称似乎并没有关系...此外,我看到你只有在发布后才发表评论... – MByD 2011-04-20 17:13:02

+0

我明白我的所作所为。谢谢。现在我必须弄清楚访问'assetManager'的最好方法是什么。 – user717555 2011-04-20 18:45:35