Game Development Reference
In-Depth Information
tected. This also allows us to completely eliminate the
if(collisionDetect)
structure as
well.
Before this optimization, we had 6 lines of code in the
.checkCollision()
method,
and 19 lines of code in the .collide() method. After the optimization, we have 5 less
lines of Java code, with 10 lines of code in each method. The new Java structures for
these methods are error-free, as shown in
Figure 16-41
, and look like the following
Java code:
public void
checkCollision
() {
for(int i=0;
i<invinciBagel.castDirector.getCurrentCast().size(); i++)
{
Actor
object
=
invinciBagel.castDirector.getCurrentCast().get(i);
If (
collide
(
object
)) {
invinciBagel.playiSound0();
invinciBagel.castDirector.addToRemovedActors(
object
);
invinciBagel.root.getChildren().remove(
object
.getSpriteFrame());
invinciBagel.castDirector.resetRemovedActors();
}
}
}
@Override
public
boolean collide
(Actor
object
) {
if
(invinciBagel.iBagel.spriteFrame.getBoundsInParent().intersects(
object
.getSpriteFrame().getBoundsInParent() ) ) {
Shape intersection =
SVGPath.intersect(invinciBagel.iBagel.getSpriteBound(),
object
.getSpriteBound());
if
(intersection.getBoundsInLocal().getWidth() !=
-1
) {
return true;
}
}
return false;
}