Game Development Reference
In-Depth Information
List<Actor> object, so that we can ascertain if any of the cast members
intersect
(have
collided with) the primary InvinciBagel character on the game Stage.
Inside of this
.checkCollision()
method, we will need a Java
for “counter” loop
structure. This structure will be used to traverse through your CURRENT_CAST
List<Actor> array of objects; in this case, these are
Prop
objects, to check for any col-
lisions (intersections) with the InvinciBagel object. The first part of the for loop is the
iterator variable declaration and initialization, in our case,
int i=0;
to declare the
integer type
iterator
variable named “i,” initialized to a count value of zero.
The second part of the for loop is the
iteration condition
, which, in this case, is “it-
erate until you reach the end of the CURRENT_CAST List<Actor> object, or
i<in-
vinciBagel.castDirector.getCurrentCast().size()
that represents
the size of (and thus, the last item in) the List. The third part of the for loop statement
condition is the amount to iterate by, and since we want to go over each object in the
List<Actor> to check for collisions, we will use the familiar
Java ++ operator
on the
“i” variable, or
i++
. Inside of the for loop {…} curly braces are the things that we want
to perform during each iteration of the loop; in this case, this would be for each Prop
Actor object that is in the List<Actor> object. This is a good construct to use to iterate
any List objects from the first element to the last element.
Inside of the for loop, we are going to create a “local” Actor object reference vari-
able, which we are going to set equal to the
current element
in the
List(i)
, using the
invinciBagel.castDirector.getCurrentCast().get(i)
method
chain. Java method chains are a cool way to create compact Java code structures, aren't
they? Once we have loaded that Actor object with the CURRENT_CAST(i) cast mem-
ber object, we will then call the
.collide()
method, using the collide(object); Java state-
ment. Remember that we installed this abstract .collide() method in our Hero super-
class, so we are going to have to “Override” and code that method next, after we learn
more about the
Node
and
Shape
classes, and a couple of their key methods and proper-
ties that can be used to determine intersections (collisions).
public void checkCollision() {
for(
int i=0
;
i
<
invinciBagel.castDirector.getCurrentCast
().size()
; i++)
{
Actor
object
=
invinciBagel.castDirector.getCurrentCast()
.get(i)
;
collide
(object);
}
}