无法在libgdx中创建动画

时间:2015-07-04 23:59:05

标签: animation graphics libgdx game-engine

我试图创建动画但不断出错

package com.leopikinc.bobdestroyer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class MainMenu implements Screen{

    Texture background_main;
    TextureRegion[] background_textures;
    Animation background_animation;
    SpriteBatch batch;
    TextureRegion current_frame; 
    float stateTime;
    BobDestroyer game;

    public MainMenu(BobDestroyer game){
       this.game = game;
}

    @Override
    public void show() {
        background_main = new Texture(Gdx.files.internal("main_menu_screen/Background.png"));
        //TextureRegion[][] temp = new TextureRegion.split(background_main, 128, 72);
        //background_textures = new TextureRegion[3];
        for(int i = 0; i<3; i++){
            background_textures[i] = new TextureRegion(background_main,0, 0+72*i, 128, 72+72*i);
        }
        background_animation = new Animation(0.2f,background_textures);
    }

    @Override
    public void render(float delta) {
    //  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        stateTime += Gdx.graphics.getDeltaTime(); 
        current_frame = background_animation.getKeyFrame(stateTime, true);
        batch.begin();
            batch.draw(current_frame, 0, 0);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }

}

这是我的代码然后我尝试启动它我得

  

线程中的异常&#34; LWJGL应用程序&#34;显示java.lang.NullPointerException       在com.leopikinc.bobdestroyer.MainMenu.show(MainMenu.java:31)       在com.badlogic.gdx.Game.setScreen(Game.java:61)       在com.leopikinc.bobdestroyer.BobDestroyer.create(BobDestroyer.java:11)       在com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)       在com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:120)

如何解决这个问题?而且当我使用TextureRegion[][] temp = new TextureRegion.split(background_main, 128, 72); Eclipse说

  

TextureRegion.split无法解析为类型

1 个答案:

答案 0 :(得分:0)

  

TextureRegion.split无法解析为类型

由于您使用的是关键字new

,因此收到错误消息

执行此类操作并修复错误

 TextureRegion[][] tmp = TextureRegion.split(background_main, background_main.getWidth()/TOTAL_COLS, background_main.getHeight()/TOTAL_ROWS);

您的第二个错误是因为您已注释了初始化阵列的行,您需要取消注释

//background_textures = new TextureRegion[3];