无法在 libgdx 中创建动画
Can't create animation in 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() {
}
}
这是我的代码,然后我尝试启动它,我得到
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.leopikinc.bobdestroyer.MainMenu.show(MainMenu.java:31)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.leopikinc.bobdestroyer.BobDestroyer.create(BobDestroyer.java:11)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:120)
如何解决这个问题?而且当我使用 TextureRegion[][] temp = new TextureRegion.split(background_main, 128, 72);
Eclipse 时说
TextureRegion.split cannot be resolved to a type
TextureRegion.split cannot be resolved to a type
您收到错误消息是因为您使用了关键字 new
做这样的事情,你的错误就会得到修复
TextureRegion[][] tmp = TextureRegion.split(background_main, background_main.getWidth()/TOTAL_COLS, background_main.getHeight()/TOTAL_ROWS);
你的第二个错误是因为你注释了初始化数组的行,你需要取消注释
//background_textures = new TextureRegion[3];
我正在尝试创建动画但总是出现错误
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() {
}
}
这是我的代码,然后我尝试启动它,我得到
Exception in thread "LWJGL Application" java.lang.NullPointerException at com.leopikinc.bobdestroyer.MainMenu.show(MainMenu.java:31) at com.badlogic.gdx.Game.setScreen(Game.java:61) at com.leopikinc.bobdestroyer.BobDestroyer.create(BobDestroyer.java:11) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:120)
如何解决这个问题?而且当我使用 TextureRegion[][] temp = new TextureRegion.split(background_main, 128, 72);
Eclipse 时说
TextureRegion.split cannot be resolved to a type
TextureRegion.split cannot be resolved to a type
您收到错误消息是因为您使用了关键字 new
做这样的事情,你的错误就会得到修复
TextureRegion[][] tmp = TextureRegion.split(background_main, background_main.getWidth()/TOTAL_COLS, background_main.getHeight()/TOTAL_ROWS);
你的第二个错误是因为你注释了初始化数组的行,你需要取消注释
//background_textures = new TextureRegion[3];