2

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 ?

xoned
  • 2,701
  • 2
  • 31
  • 43

2 Answers2

3

The original answer, I feel, is a really pedantic. Fabian is right, there are essentially two routes to rendering in JavaFX. One is add nodes to other nodes, VBoxs onto Groups etc. The second is via the canvas. Some games, and other performance related applications avoid nodes and the scenegraph as much as possible. The whole issue of the speed of the scenegraph is somewhat contentious.

And to actually answer the question: no, in many cases, using the scenegraph and nodes is not efficient currently. The original answer attempts to confuse by stating that these elements are in fact all nodes. However, this is not a helpful answer. The canvas allows low level drawing operations such as rendering images, primitives etc. It offers blending modes, and opacity based operations. However, to access certain types of text formatting, taking snapshots etc, it is useful to dip into the node. Then if you are using purely the canvas to convert these results back into images.

wax_lyrical
  • 922
  • 1
  • 10
  • 22
0

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.

DVarga
  • 21,311
  • 6
  • 55
  • 60
  • 1
    Your claim is true but nontheless there are alternatives to using one node per game object such as `Canvas`, which your answer just ignores... – fabian May 15 '16 at 13:35
  • So can I just use Nodes for the game UI and the Canvas for the actual game levels ? That might be good, right ? – QuantumCookie May 15 '16 at 15:19
  • Yes, you can put Images to Canvas, therefore you you can use this teqnique for game levels, as in the tutorial in the link. It would be interesting, if you have a working game with ImageViews, create a branch, re-implement it to use Canvas, then see the difference in performance:) – DVarga May 16 '16 at 11:12
  • Thank you :) . I noticed a small amount of performance improvement when I used Canvas. It's also easier because I'm used to rendering with the Graphics object when I was making Swing games. I'll reserve Nodes for the GUI .Thanks again ! – QuantumCookie May 16 '16 at 11:21