Je suis en train de créer un jeu 2D en Java avec libGDX. Je suis resté sur la création d'animation 2D à partir de feuille de sprite dans un fichier PNG.libGDX sprite sheet 2d animation
J'ai la classe AbstractScreen qui contient des 'choses' pour chaque écran.
Classe GameScreen pour dessiner mon jeu et la classe Player avec le code d'animation.
La classe de lecteur est de https://github.com/libgdx/libgdx/wiki/2D-Animation exemple.
J'ai d'abord créé AbstractScreen et MainMenuScreen (basé sur AbstractScreen). Ça marche mais maintenant j'essaye de créer GameScreen avec mon lecteur animé. Quelque chose ne va pas parce que je n'ai aucune erreur et le joueur n'est pas visible sur l'écran de l'application. Comment mettre en œuvre correctement l'animation feuille de sprite pour le squelette de mon application?
Mon écran de jeu:
class GameScreen extends AbstractScreen {
Player player;
public GameScreen(NinjaGame game) {
super(game);
init();
}
@Override
protected void init() {
initPlayer();
}
private void initPlayer() {
player = new Player();
player.setDebug(true);
stage.addActor(player);
}
@Override
public void render(float delta) {
super.render(delta);
update();
spriteBatch.begin();
stage.draw();
spriteBatch.end();
}
private void update() {
stage.act();
}
}
Ma classe AbstractScreen:
public abstract class AbstractScreen implements Screen {
protected NinjaGame game;
protected Stage stage;
private OrthographicCamera camera;
protected SpriteBatch spriteBatch;
public AbstractScreen(NinjaGame game) {
this.game = game;
createCamera();
/* Stage for actors */
stage = new Stage(new StretchViewport(NinjaGame.SCREEN_WIDTH, NinjaGame.SCREEN_HEIGHT, camera));
/* Batch for sprites */
spriteBatch = new SpriteBatch();
/* Stage takes user inputs */
Gdx.input.setInputProcessor(stage);
init();
}
protected abstract void init();
private void createCamera() {
/* Orthographic means like in CAD drawings */
camera = new OrthographicCamera();
camera.setToOrtho(false, NinjaGame.SCREEN_WIDTH, NinjaGame.SCREEN_HEIGHT);
camera.update();
}
/** Clean screen on black color between render frames */
private void clearScreen() {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
@Override
public void render(float delta) {
clearScreen();
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
}
}
Ma classe de joueur:
public class Player extends Actor {
private static final int FRAME_COLS = 10, FRAME_ROWS = 1;
Animation<TextureRegion> walkAnimation;
Texture walkSheet;
private final static int STARTING_X = 50;
private final static int STARTING_Y = 50;
public Player(){
createIdleAnimation();
this.setPosition(STARTING_X, STARTING_Y);
}
private void createIdleAnimation() {
walkSheet = new Texture(Gdx.files.internal("sheets/ninja-idle-sheet.png"));
TextureRegion[][] tmp = TextureRegion.split(walkSheet,
walkSheet.getWidth()/FRAME_COLS,
walkSheet.getHeight()/FRAME_ROWS);
TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}
walkAnimation = new Animation<TextureRegion>(0.025f, walkFrames);
}
@Override
public void act(float delta) {
super.act(delta);
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
}
}
MISE À JOUR 1
NinjaGame class
public class NinjaGame extends Game{
public final static int SCREEN_WIDTH = 800;
public final static int SCREEN_HEIGHT = 480;
public final static String GAME_NAME = "ninja vs zombie";
private boolean paused;
@Override
public void create() {
this.setScreen(new GameScreen(this));
}
}
avec votre code de classe joueur j'ai écran toujours vide sans animation joueur @Abhishek aryenne – twistezo
s'il vous plaît montrer votre 'NinjaGame' classe? – Aryan
voir mise à jour 1 @Abhishek Aryan – twistezo