28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11 ■ Moving Your Action Figure in 2D: Controlling the X and Y Display Screen Coordinates<br />

You could also, later on, create a method for Image sprite List object loading. This would pass the<br />

ArrayList as a parameter, instead of a comma-delimited List. Note that if you did this, you would also need to change<br />

your Actor abstract class constructor to take in an ArrayList object, instead of an Image... List of Image objects.<br />

Next, let’s take a look at how we add our newly created iBagel object into our game’s Scene Graph object, which is<br />

currently a Stackpane object named root.<br />

Adding Your iBagel to the Scene Graph: .addGameActorNodes()<br />

One of the steps that <strong>Java</strong>FX application developers often forget is to add their objects that will need to be displayed<br />

in the Scene (and on the Stage that the Scene object is attached to) to the root object of the Scene Graph. In our case,<br />

this is a StackPane object named root. We will need to use the same root.getChildren().add() method call chain<br />

that we did in Chapter 6, when we started developing our Splashscreen, to add our iBagel object ImageView, which<br />

is referenced using iBagel.spriteFrame, to the Scene Graph root object. I am going to add a method at this stage that<br />

will ensure that we never forget this important add to Scene Graph step in our work process. I am going to specifically<br />

address this stage in the Actor creation work process by making it into its own method, which I am going to call<br />

.addGameActorNodes(). The creation of this method body, and our first add Actor to Scene Graph programming<br />

statement, would be accomplished using the following <strong>Java</strong> code, which is also shown (highlighted) in Figure 11-7:<br />

private void addGameActorNodes() {<br />

root.getChildren().add(iBagel.spriteFrame);<br />

}<br />

Figure 11-7. Create a private void addGameActorNodes() method; .add() iBagel.spriteFrame ImageView to root object<br />

It is important to note that I am calling this .addGameActorNodes() method, at the top of the InvinciBagel class,<br />

inside of the .start() method, before I call the .addNodesToStackPane() method. There is a very good reason for doing<br />

this, and it goes back to what you learned about <strong>Java</strong>FX in Chapter 4. Remember the objects that you add to the<br />

StackPane layer management object are displayed using a Z-index (or Z-order), which means that they are “stacked”<br />

on top of each other. If any of these layers do not have an alpha channel, which we learned about in Chapter 5, then<br />

nothing behind them will be able to show through! For this reason, the easiest way to get our Splashscreen to overlay<br />

our game at any point in time that a player clicks the INSTRUCTIONS Button control object is to add these assets last.<br />

www.it-ebooks.info<br />

237

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!