gpt4 book ai didi

java - 初始化 RevoluteJointDef 时出现 NullPointerExceptions

转载 作者:行者123 更新时间:2023-11-30 02:47:28 25 4
gpt4 key购买 nike

我已经开始深入研究 AndEngine 的更复杂用途,因为我开始掌握它,或者我是这么想的。我遇到了一个我似乎无法弄清楚的 NPE,因此我将不胜感激任何帮助。

下面的类代表一个粗糙的布娃娃/火柴人,类本身用 andengine 注册了它的所有纹理、 body 、关节等等。 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--
}

希望这不是我所做的明显愚蠢的事情。如果是,请见谅。

最佳答案

您似乎隐藏了几个变量,包括您的 sSkates、bSkates、jAnkle 变量。这是您在类中以及在构造函数、方法或其他局部 block 中再次声明变量的地方。我看不出这是如何在构造函数导致您的 NPE,但这是其他地方 NPE 的常见原因。

关于java - 初始化 RevoluteJointDef 时出现 NullPointerExceptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24792975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com