Game Development Reference
In-Depth Information
Exploring Scene2D UI, TableLayout, and
skins
LibGDX comes with a great feature set to easily create scene graphs. A scene graph
is a hierarchically organized structure of objects similar to files and folders on a
hard disk. In LibGDX, such objects are called actors. Actors can be nested to create
logical groups. Grouping actors is a very useful feature, as modifications applied to a
parent actor will also affect its child actors. Furthermore, each actor has its own local
coordinate system, which makes it very easy to define relative offsets inside a group
of actors, including position, angle of rotation, and scale.
Scene2D supports hit detection of rotated and scaled actors. LibGDX's flexible
event system allows you to handle and route inputs as needed so that the parent
actors can intercept inputs before they reach the child actors. Finally, the built-in
action system can be used to easily manipulate actors over a period of time, creating
complex effects that can execute in sequence, parallel, or in a combination of both.
All this described functionality is encapsulated in the Stage class, which contains
the hierarchy and distributes user-generated events. Actors can be added to and
removed from it at any time. The Stage class and the Actor class both contain an
act() method, which takes a delta time as its argument to do a time-based action.
Calling act() on a Stage instance will cause a call of act() on every actor in the
scene graph. The act() methods of Stage and Actor are basically what we already
know as the update() methods, only using a different name. For more information
on Scene2D, check out the official documentation at https://github.com/libgdx/
libgdx/wiki/Scene2d/ .
Until now, we have not used any of Scene2D's functionality in our game. Naturally,
we could have implemented the game world, including its game objects, with
Scene2D. However, always keep in mind that using a scene graph comes with a
certain amount of overhead. LibGDX tries its best to keep the overhead at a bare
minimum, such as skipping complex calculations of transformation matrices if
objects do not need to be rotated or scaled. So, it really depends on what your
requirements are.
As the user interface of the menu that we are going to create is rather complex, we
want to make use of LibGDX's scene graph for this task. More precisely, we will
use Scene2D UI. This is another implementation in LibGDX that builds on top of
Scene2D and extends its functionality by providing a rich set of common and ready-
to-use UI elements. In LibGDX, these UI elements are called widgets .
 
Search WWH ::




Custom Search