【问题标题】:Can't create animation in libgdx无法在 libgdx 中创建动画
【发布时间】:2015-09-22 10:24:42
【问题描述】:

我正在尝试创建动画,但不断出错

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() {

    }

}

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

线程“LWJGL 应用程序”中的异常 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 无法解析为类型

【问题讨论】:

  • 您注释掉了实例化 TextureRegion 数组的行,因此它为空。关于你的第二个问题,你需要去掉new这个词。

标签: animation graphics libgdx game-engine


【解决方案1】:

TextureRegion.split 无法解析为类型

因为您使用了关键字new,所以出现错误

做这样的事情,你的错误就会得到解决

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

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

//background_textures = new TextureRegion[3];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 2021-10-29
    • 2021-11-29
    相关资源
    最近更新 更多