2017-04-25 8 views
-3

Ich bekomme eine NullPointerException, wenn ich versuche, meinen Player zu erstellen und ihn zu animieren. Ich verstehe nicht wirklich was falsch ist. Hier ist der Code:NullPointerException ist Java beim Versuch, mit libgdx zu animieren

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; 
    } 
} 

Und die Stacktrace ist:

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

Mögliche Duplikat angeordnet [Was für eine Nullpointer ist, und wie kann ich es beheben?] (Http: // Stackoverflow .com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – csmckelvey

+0

'Ausnahme im Thread" LWJGL Application "java.lang.NullPointerException unter com.sheep.game.Sprites. Player.update (Player.java:53) 'Welche Zeile ist 53? Was könnte dort null sein? Wenn Sie in diesem Bereich debuggen, was finden Sie? – csmckelvey

+0

Überprüfen Sie, ob animationFrames ordnungsgemäß initialisiert sind. – Serhiy

Antwort

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 
    } 
} 

von oben Schleife nur Sie ersten Index initialisieren, nachdem Schleife animationFrames[1] null ist

Ausnahme hier

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

Einstellung null TextureRegion bis Sprite.

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

EDIT

Wenn beide Sprite Rahmen horizontal

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

Ich lerne gerade erst und ich verstehe nicht wirklich was du meinst? Wie behebe ich das? – Cjinks

+0

Wie viele Sprite Frames in sheepLoop2.png? – Aryan

+0

Es gibt zwei Bilder beide sind 33 x 21 Pixel – Cjinks

Verwandte Themen