所以这是一个难题:
我正在使用两个不同的类和两个不同的包编写程序。但是,当我尝试从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");
而且,我确实导入了课程。
最佳答案
loadModel
是一种非静态方法。您需要从类的对象实例中调用它。
另外,即使它是静态方法,也应从其类中调用它,例如BaseGame.loadModel("somestring");
编辑完答案后,我认为这显然是问题所在:
您可以在main(BaseGame
)中实例化一个BaseGame app = new BaseGame();
对象,但似乎没有实现任何特殊的对象。
您在一个单独的包中有一个单独的类,该类没有对BaseGame
实例的引用,因为没有人将这样的引用传递给它,并且import
不是实例化。
关于java - Java程序中的“找不到符号”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5734005/