繁体   English   中英

(LibGDX)-调用角色的render方法时获取NullPointerException

[英](LibGDX) - getting NullPointerException when calling my character's render method

无法弄清楚为什么会这样,以前很好。 基本上,游戏会启动并显示一秒钟,我可以看到我的tilemap和角色然后崩溃,LogCat为我提供了Spritebatch的空指针,并且代码中指向的第一件事是我的renderFrog()方法。 这是相关的代码(我知道它很杂乱,但是我只是想将其一起破解,在我得到这个基本的工作原型之后,将实现干净的实现和类层次结构):

@Override
// my create method, just loading an animation in. this worked previously, i haven't changed anything on it.    


public void create() {

    batch = new SpriteBatch();
    textureAtlas = new   TextureAtlas(Gdx.files.internal("data/froganimate.atlas"));
    TextureRegion[] walkLeftFrames = new TextureRegion[4];
    for (int i = 0; i < 3; i++) {
                    walkLeftFrames[i] = textureAtlas.findRegion("000" + (i + 7));
                    walkLeftFrames[i].flip(true, false);

                }
    walkLeftAnimation = new Animation(0.16f, walkLeftFrames);

    TextureRegion[] walkRightFrames = new TextureRegion[4];
    for (int i1 = 0; i1 < 3; i1++) {
                    walkRightFrames[i1] = textureAtlas.findRegion("000" + (i1 + 7));

                }
    walkRightAnimation = new Animation(0.04f, walkLeftFrames);

}

//the render method. as far as i know, i didn't change anything on it since it broke.

@Override
    public void render() {      
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        stateTime += Gdx.graphics.getDeltaTime();
        updateFrog(stateTime);

        cam.update();
        renderer.setView(cam);

        renderer.render();
        renderFrog(stateTime);
    }

// the update frog method. the keystates are activated by touches on the screen

    private void updateFrog(float deltaTime){
        if(deltaTime == 0) return;
        frog.frogTime += deltaTime;


        if (keystates.get(Keystates.LEFT))
        {
            frog.state = Frog.State.WalkingL;
            frog.velocity.x = -Frog.MAX_VELOCITY;
        }
        if (keystates.get(Keystates.RIGHT))
        {
            frog.state = Frog.State.WalkingR;
            frog.velocity.x = Frog.MAX_VELOCITY;
        }
        if (keystates.get(Keystates.UP))
        {
            frog.state = Frog.State.WalkingU;
            frog.velocity.y = -Frog.MAX_VELOCITY;
        }
        if (keystates.get(Keystates.DOWN))
        {
            frog.state = Frog.State.WalkingD;
            frog.velocity.y = Frog.MAX_VELOCITY;
        }

        if(Math.abs(frog.velocity.x) < 1 & Math.abs(frog.velocity.y) < 1)
        {
            frog.state = Frog.State.Standing;

        }

        {
        if(Math.abs(frog.velocity.x) > Frog.MAX_VELOCITY) {
            frog.velocity.x = Math.signum(frog.velocity.x) * Frog.MAX_VELOCITY;
    }
        if(Math.abs(frog.velocity.x) < .005f){
            frog.velocity.x = 0;




        }
        if(Math.abs(frog.velocity.y) > Frog.MAX_VELOCITY) {
            frog.velocity.y = Math.signum(frog.velocity.y) * Frog.MAX_VELOCITY;
    }
        if(Math.abs(frog.velocity.y) < .005f){
            frog.velocity.y = 0;
        }

        frog.velocity.scl(deltaTime);
        frog.position.add(frog.velocity);
        frog.velocity.scl(1/deltaTime);






    }
    }
}

private void renderFrog(float deltaTime){


    TextureRegion frogregion = null;


    switch(frog.state){
    case WalkingU: frogregion = walkLeftAnimation.getKeyFrame(frog.frogTime); break;
    case WalkingD: frogregion = walkLeftAnimation.getKeyFrame(frog.frogTime); break;
    case WalkingL: frogregion = walkLeftAnimation.getKeyFrame(frog.frogTime); break;
    case WalkingR: frogregion = walkLeftAnimation.getKeyFrame(frog.frogTime); break;
    case Standing: frogregion = walkLeftAnimation.getKeyFrame(frog.frogTime); break;
    }


    frogregion = walkLeftAnimation.getKeyFrame(frog.frogTime)

    SpriteBatch batch = renderer.getSpriteBatch();
    batch.begin();
// THIS IS WHERE THE ERROR HAPPENS      
batch.draw(frogregion, frog.position.x, frog.position.y, Frog.WIDTH, Frog.HEIGHT);

    batch.end();
}

预编辑:我尝试在create()方法中初始化“ frogregion”,但这没有任何改变。 救命!

在2个动画中,您具有:

TextureRegion[] walkLeftFrames = new TextureRegion[4];
    for (int i = 0; i < 3; i++) {

TextureRegion[] walkRightFrames = new TextureRegion[4];
    for (int i1 = 0; i1 < 3; i1++) {

您创建了一个由4个TextureRegions组成的数组,但仅创建了前3个(i = 0,i = 1和i = 2,因为条件是i <3)。 将其更改为<4:

TextureRegion[] walkLeftFrames = new TextureRegion[4];
    for (int i = 0; i < 4; i++) {

TextureRegion[] walkRightFrames = new TextureRegion[4];
    for (int i1 = 0; i1 < 4; i1++) {

(或者,如果每个动画它们只有3帧,则将所有4帧更改为3帧)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM