28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

In this section, you will first explore decision-making control structures, such as the <strong>Java</strong> switch-case<br />

structure and the if-else structure. Then, you will take a look at <strong>Java</strong>’s looping control structures, including for,<br />

while, and do-while.<br />

Decision-Making Control Structures: Switch-Case and If-Else<br />

Some of the most powerful <strong>Java</strong> logic control structures allow you to define decisions that you want your program<br />

logic to make for you as the application is running. One such structure offers a case-by-case, “flat” decision matrix; the<br />

other has a cascading (if this, do this; if not, do this; if not, do this; and so on) type of structure that evaluates things in<br />

the order in which you want them examined.<br />

Let’s start by looking at the <strong>Java</strong> switch statement, which uses the <strong>Java</strong> switch keyword and an expression at the<br />

top of this decision tree and then uses the <strong>Java</strong> case keyword to provide <strong>Java</strong> statement blocks for each outcome for<br />

this expression’s evaluation. If none of the cases inside a switch statement structure (curly braces) are called (used)<br />

by the expression evaluation, you can also supply a <strong>Java</strong> default keyword and <strong>Java</strong> statement code block for what you<br />

want done.<br />

The variable used in the case statements can be one of four <strong>Java</strong> data types: char (character), byte, short, or int<br />

(integer). You will generally want to add a <strong>Java</strong> break keyword at the end of each of your case statement code blocks,<br />

at least in the use case, in which the values being switched between need to be exclusive, and only one is viable (or<br />

permissible) for each invocation of the switch statement. The default statement, which is the “if any of these do not<br />

match” is the last of the statements inside of the switch, and does not need this break keyword.<br />

If you do not furnish a <strong>Java</strong> break keyword in each of your case logic blocks, more than one case statement can<br />

be evaluated in the same pass through your switch statement. This would be done as your expression evaluation tree<br />

progresses from top (first case code block) to bottom (last case code block or default keyword code block). So if you<br />

had a collection of Boolean “flags” such as hasValue, isAlive, isFixed, and so on, these could all be processed on one<br />

single “pass” by using a switch-case statement structure that does not use any break statements at all.<br />

The significance of this is that you can create some fairly complex decision trees, based on case statement<br />

evaluation order, and whether you put this break keyword at the end of any given case statement’s code block.<br />

The general format for your switch-case decision tree programming construct would look like this:<br />

switch(expression) {<br />

case value1 :<br />

programming statement one;<br />

programming statement two;<br />

break;<br />

case value2 :<br />

programming statement one;<br />

programming statement two;<br />

break;<br />

default :<br />

programming statement one;<br />

programming statement two;<br />

}<br />

Let’s say you want to have a decision in your game as to which InvinciBagel death animation is called when<br />

the InvinciBagel is hit (shot, slimed, punched, and so on). The death animation routine (method) would be called,<br />

based on the InvinciBagel’s state of activity when he or she is hit, such as flying (F), jumping (J), running (R), or idle<br />

66<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!