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 14 ■ Setting Up the Game Environment: Creating Fixed Sprite Classes Using the Actor Superclass<br />

If you still see that wavy red error highlight, once you have imported the Image class reference, it is because you<br />

need to pass the parameter list in your Prop() constructor method definition “up to” the Actor class. This is done using<br />

the <strong>Java</strong> super keyword, sometimes called a super() constructor (method), using the following <strong>Java</strong> statement:<br />

super(SVGdata, xLocation, yLocation, spriteCels);<br />

Since this Prop class uses a default, or unflipped (unmirrored) imageStates(0) image for the class, this is the<br />

first thing that we need to do to make a usable Prop class that is in conformance with the abstract Actor class. Also,<br />

remember that the Actor class initializes all of the flag properties for us, thanks to a detailed design process. The <strong>Java</strong><br />

code for the class, which now includes the basic super constructor, and is now error-free, can be seen in Figure 14-7:<br />

Figure 14-7. Add the super() constructor method call inside of the Prop() constructor, to get rid of the error highlight<br />

The next thing that we will need to do, to position the fixed sprite, using the xLocation and yLocation values that<br />

are passed into the constructor method, is to use the .setTranslateX() and .setTranslateY() methods. Remember that<br />

you utilized these methods in Chapter 12 in the .moveInvinciBagel() method. We’ll use these again, in this Prop()<br />

constructor method, to position these fixed sprites on the Stage where your constructor method parameters instruct<br />

the object constructor to locate them on the screen.<br />

It is important to remember that because of the work we did in Chapter 8 that the Actor superclass’s Actor()<br />

constructor method already performs the iX=xLocation; and iY=yLocation; sprite iX and iY property setting for us.<br />

Thus, all that we have to do in the Prop() constructor method is to call spriteFrame.setTranslateX(xLocation);<br />

and spriteFrame.setTranslateY(yLocation); inside of the constructor method, and after our super() constructor<br />

method call. Notice in the code that the xLocation and yLocation variables are utilized in both the super() constructor<br />

method call to set the iX and iY properties for the Prop Actor as well as inside the .setTranslateX() and .setTranslateY()<br />

method calls, to position the fixed sprites on the Stage during the Prop object instantiation, so that we do not have to<br />

do this somewhere else in our code. The <strong>Java</strong> code for the class, and constructor method, will look like the following:<br />

package invincibagel;<br />

import javafxscene.image.Image;<br />

public class Prop extends Actor {<br />

www.it-ebooks.info<br />

303

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

Saved successfully!

Ooh no, something went wrong!