【发布时间】:2014-07-17 00:22:08
【问题描述】:
我已经开始深入研究 AndEngine 的更复杂用途,因为我开始掌握它的窍门,或者我是这么认为的。我遇到了一个我似乎无法弄清楚的 NPE,因此我将不胜感激。
下面的类代表一个粗制滥造的布娃娃/火柴人,该类本身使用andengine注册了它的所有纹理、身体、关节等等。 mainActivity(扩展了 SimpleBaseGameActivity)然后在初始化期间以这种方式调用其构造函数:
this.skater = new Skater(this, mPhysicsWorld, new Vector2(startX, startY));
我已经删除了一些代码,这些代码主要是对其他关节和四肢重复相同的内容,因此只包括踝关节的内容,因为文本墙已经足够大了。
NPE 被抛出到构造函数末尾的这一行:
this.jAnkle.initialize(bSkates, bLegs, new Vector2(stickmanPos.x, sSkates.getHeight()));
public class Stickman {
//////////////
// Constructor
public Stickman(Testgame game, PhysicsWorld engine, Vector2 stickmanPos) {
this.game = game;
this.engine = engine;
this.stickmanPos = stickmanPos;
// Create textures
textureAtlas = new BitmapTextureAtlas(game.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
this.tSkates = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.textureAtlas, this.game, "stickman_skates.png", 0, 0, 2, 1);
this.tLegs = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.textureAtlas, this.game, "stickman_legs.png", 0, 0, 2, 1);
// --SNIPPED--
// Create sprites
this.sSkates = new AnimatedSprite(stickmanPos.x, stickmanPos.y, this.tSkates, game.getVertexBufferObjectManager());
this.sLegs = new AnimatedSprite(stickmanPos.x + skatesToLegs, stickmanPos.y, this.tLegs, game.getVertexBufferObjectManager());
// --SNIPPED--
// Create body with sprites
this.bSkates = PhysicsFactory.createBoxBody(engine, sSkates, BodyType.DynamicBody, game.FIX_SKATER);
this.bLegs = PhysicsFactory.createBoxBody(engine, sLegs, BodyType.DynamicBody, game.FIX_SKATER);
// --SNIPPED--
// Register bodies with Box2D
this.engine.registerPhysicsConnector(new PhysicsConnector(this.sSkates, this.bSkates, true, true));
this.engine.registerPhysicsConnector(new PhysicsConnector(this.sLegs, this.bLegs, true, true));
// --SNIPPED--
// Create joints
RevoluteJointDef jAnkle = new RevoluteJointDef();
System.out.println("aoeu aoeu aoeu aoeu");
this.jAnkle.initialize(bSkates, bLegs, new Vector2(stickmanPos.x, sSkates.getHeight()));
this.jAnkle.enableMotor = true;
this.jAnkle.motorSpeed = this.dex;
this.jAnkle.maxMotorTorque = this.str;
}
////////////////
// Fields
private Testgame game;
private PhysicsWorld engine;
private Vector2 stickmanPos;
// Config
private float skatesToLegs = 1;
private float legsToThigh = 1;
private float thighToTorso = 1;
private float torsoToHead = 1;
private float torsoToArms = 1;
// Stats
private float gli = 0; // Higher number = lower skate friction
private float str = 0; // Joint torque
private float dex = 0; // Joint speed
private float con = 0; // Impact tolerance
// JointPositions
private float shoulderPos;
private float neckPos;
private float hipPos;
private float kneePos;
protected BitmapTextureAtlas textureAtlas;
protected ITiledTextureRegion tSkates;
protected ITiledTextureRegion tLegs;
// --SNIPPED--
protected AnimatedSprite sSkates;
protected AnimatedSprite sLegs;
// --SNIPPED--
protected Body bSkates;
protected Body bLegs;
// --SNIPPED--
protected RevoluteJointDef jAnkle;
// --SNIPPED--
}
希望这不是我所做的明显愚蠢的事情。如果是,请见谅。
【问题讨论】:
-
我可以看到您正在隐藏 jAnkle 变量,但我看不出这是如何导致您的 NPE 被抛出的。
-
@HovercraftFullOfEels 你说的阴影是什么意思
-
可能火柴人对象为空
-
你已经在
protected RevoluteJointDef jAnkle;的类中声明了变量,但在构造函数中重新声明了它。 -
@JunedAhsan:???这是 Stickman 构造函数。
标签: java android nullpointerexception andengine