1

无法弄清楚为什么会发生这种情况,以前很好。基本上游戏启动并显示一秒钟,我可以看到我的 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”,但这并没有改变任何东西。帮助!

4

1 回答 1

2

在 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)。

于 2013-11-09T04:24:06.467 回答