2017-04-25 51 views
-3

我是说我想创建我的球员,他的动画一个NullPointerException动画。我真的不明白什么是错的。 下面是代码:NullPointerException异常是Java的尝试与libgdx

package com.sheep.game.Sprites; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.Animation; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.sheep.game.Screens.PlayScreen; 

public class Player extends Sprite { 

    float elapsedTime = 0; 

    public Animation<TextureRegion> animation; 
    public Texture sheepLoop2; 
    TextureRegion[] animationFrames; 

    public Sprite spr_player; 

    private PlayScreen screen; 

    public Player(PlayScreen screen) { 

     this.screen = screen; 

     sheepLoop2 = new Texture("sheepLoop2.png"); 

     TextureRegion[][] frames = TextureRegion.split(sheepLoop2, 33, 21); 

     animationFrames = new TextureRegion[2]; 
     int index = 0; 

     for(int i = 0; i < 1; i++) { 
      for(int j = 0; j < 1; j++) { 
       animationFrames[index++] = frames[j][i]; 
      } 
     } 

     animation = new Animation<TextureRegion>(1f/30f, animationFrames); 

     spr_player = new Sprite(sheepLoop2); 



    } 

    public void update() { 

     spr_player.setX(10); 
     spr_player.setY(100); 
     spr_player.setRegion(animation.getKeyFrame(elapsedTime,true)); 

    } 

    public void render() { 

     elapsedTime += Gdx.graphics.getDeltaTime(); 

    } 

    public Texture getSheepLoop2() { 
     return sheepLoop2; 
    } 
} 

而且堆栈跟踪是:

Exception in thread "LWJGL Application" java.lang.NullPointerException 
    at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:112) 
    at com.sheep.game.Sprites.Player.update(Player.java:53) 
    at com.sheep.game.Screens.PlayScreen.update(PlayScreen.java:61) 
    at com.sheep.game.Screens.PlayScreen.render(PlayScreen.java:84) 
    at com.badlogic.gdx.Game.render(Game.java:46) 
    at com.sheep.game.SheepGame.render(SheepGame.java:28) 
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225) 
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126) 
+9

的可能的复制[什么是空指针异常,怎么解决呢?(HTTP://计算器.COM /问题/ 218384 /什么 - 是 - 一 - NullPointerException异常和知识-DO-I-FIX-IT) – csmckelvey

+0

'异常在线程com.sheep.game.Sprites “LWJGL应用” 显示java.lang.NullPointerException。 Player.update(Player.java:53)'哪一行是53?那里可能是空的?当你在该区域进行调试时,你会发现什么? – csmckelvey

+0

检查animationFrame是否已正确初始化。 – Serhiy

回答

0
animationFrames = new TextureRegion[2]; // having size 2 array initialisation not TextureRegion 

for(int i = 0; i < 1; i++) { 
    for(int j = 0; j < 1; j++) { 
     animationFrames[index++] = frames[j][i]; //run once according to your condition 
    } 
} 

通过上面的循环,你只初始化第一指标,循环后animationFrames[1]是空

异常这里

animation.getKeyFrame(elapsedTime,true) //return second TextureRegion that is still null 

将null设置为TextureRegionSprite

spr_player.setRegion(animation.getKeyFrame(elapsedTime,true)); 

编辑

如果两个精灵帧水平排列

for(int i = 0; i < 1; i++) { 
    for(int j = 0; j < 2; j++) { 
     animationFrames[index++] = frames[i][j]; 
    } 
} 
+0

我只是在学习,我不明白你的意思?我该如何解决? – Cjinks

+0

sheepLo​​op2.png中有多少个精灵帧? – Aryan

+0

有两个帧都是33 x 21像素 – Cjinks

相关问题