ไม่สามารถสร้างภาพเคลื่อนไหวใน libgdx

ฉันกำลังพยายามสร้างแอนิเมชั่นแต่กลับได้รับข้อผิดพลาดอยู่เรื่อยๆ

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 Application" 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 ไม่สามารถแก้ไขเป็นประเภทได้


person Leopik    schedule 04.07.2015    source แหล่งที่มา
comment
คุณใส่ความคิดเห็นในบรรทัดที่คุณสร้างอินสแตนซ์อาร์เรย์ TextureRegion ของคุณเพื่อให้เป็นโมฆะ สำหรับคำถามที่สองของคุณ คุณจะต้องลบคำว่า ใหม่ ออก   -  person Tenfour04    schedule 05.07.2015


คำตอบ (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];
person Sneh    schedule 05.07.2015