1

I want to make a simple 2D game. Right now everything I want to do is displaying a single sprite (player) of my sprite sheet. So my main class extends javafx.application.Application and I have a stage and a player I want to display on it.

The player class holds an array filled with single sprites I got from my sprite sheet with the method .getSubimage. In order to display something on the stage you need a scene that holds a Group or a StackPane with objects you want to display. So I tried to add my player to the scene by appending it to the StackPane called entities by entities.getChildren().add(player). That does not work.

I am not really familiar with working with stages etc. Is it the wrong way to extend my game from "Application". How can I add custom objects to the scene?

Main Class:

public class Sokobencho extends Application  {

private int height = 96;
private int width = 144;
private int scale = 4;
private Player player;
private StackPane entities;
public Stage stage;

public Sokobencho() throws IOException {
    player = new Player("res/", 0);
}

public void start(Stage stage) throws Exception {
    loadEntities();

     Scene scene = new Scene(entities, width*scale, height*scale);
        stage.setTitle("Sokobencho by Daniel Welzel");
        stage.setScene(scene);
        stage.show();

}

public void loadEntities() {
    entities.getChildren().add(player);
}


public static void main(String[] args) throws Exception {
    launch(args);
}

}

Player Class:

public class Player extends Moveable {

private int posX;
private int posY;
private Spritesheet spritesheet;
private BufferedImage sprite;

public Player(String filePath, int spriteNumber) throws IOException {
    spritesheet = new Spritesheet(filePath, 16, 16, 10, 10);
    getSprite(spriteNumber);

}

public void getSprite(int i) {
    sprite = spritesheet.getSprite(i);
}

public void moveTo(int newX, int newY) {
    posX = newX;
    posY = newY;

}
}

In case you want to know how I handled the sprite sheet:

public Spritesheet(String filepath, int spriteWidth, int spriteHeight, int spriteRows, int spriteCols) throws IOException {
    filePath = filepath;
    rows = spriteRows;
    cols = spriteCols;
    width = spriteWidth;
    height = spriteHeight;
    sprites = new BufferedImage[rows * cols];
    createSprites();
}

public void createSprites() throws IOException {
    spritesheet = ImageIO.read(new File(filePath));
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            sprites[(i * cols) + j] = spritesheet.getSubimage(
                    j * width,
                    i * height,
                    width,
                    height
                    );
        }
    }
}

public BufferedImage getSprite(int number) {
    return sprites[number];
}
}

Abstract Class Moveable:

public abstract class Moveable {

private int posX;
private int posY;
private BufferedImage sprite;

public void getSprite(int x,int y) {

}

public void moveTo(int newX, int newY) {

}

}
4
  • What is Moveable? Is it a Node subclass? And what do you mean by "That does not work"?
    – James_D
    Sep 10, 2015 at 15:39
  • @James_D I added the code. And by "that does not work" I mean I can't add the object "player" this way. It seems like it does not accept the class. On tutorials people always use things like text or shapes and append them to the Group or StackPane. But I want to know how I can add my own object.
    – Dawesign
    Sep 10, 2015 at 15:46
  • It is not a Node. You can only add Nodes to the scene graph. (What do you expect to see by adding your player to the scene graph, and how do you expect JavaFX to know that's what you want to see?)
    – James_D
    Sep 10, 2015 at 15:50
  • Well it would be actually better to call a render function inside the player class which than only adds the image of the sprite to a node which than gets added to the scene and displayed. But how do I do that? Adding the player the scene would display nothing, I got it.
    – Dawesign
    Sep 10, 2015 at 15:56

1 Answer 1

2

You can only add Node subclasses to your scene graph.

I would expose a javafx.scene.image.Image in your Player class. (You might choose to work exclusively with these, instead of BufferedImages, or you might convert using SwingFXUtils.toFXImage(...) somewhere in your code.)

So this would look like

public class Player {

    //existing code...

    public Image getSprite() {
        return ... ;
    }
}

and then you can just do

entities.getChildren().add(new ImageView(player.getSprite()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.