Search code examples
javajavafxjavafx-82d-games

Is it efficient to use Node objects as GameObjects in a JavaFX game?


I got this book : Beginning Java 8 Games Development by Wallace Jackson.

A part of the book teaches how to use Node objects ( I used the ImageView class ) as GameObjects.I'm creating a 2D Space Invaders-type game. Each GameObject has a ImageView member that contains the sprite, and the ImageView object is added to the root of the Scene. Won't having so many Nodes ( Aliens ) hog too much memory ?

What I want to know is, is using Node objects efficient, or is there a more efficient way to represent these GameObjects ?


Solution

  • If you want to display something on the screen you have no other possibility than using Node.

    The for sentence from the documentation of Node:

    Base class for scene graph nodes. A scene graph is a set of tree data structures where every item has zero or one parent, and each item is either a "leaf" with zero sub-items or a "branch" with zero or more sub-items.

    Based on the comment of @fabian:

    It is also possible to have a single-node structure, using Canvas:

    This link can be a good start to check this approach.