My latest article of the series, “Writing the Pac-Man Game in JavaFX Part 4“, was out on June 4.
In this article, the interaction between Pac-Man character and the ghosts was introduced in detail. The article showed how to determine whether the Pac-man character and a ghost touched each other. A simplified formla was applied to achieve better performance. When Pac-man touches a ghost, he can eat it if the ghost is hollow. The ghost then is thrown back to the cage again. Otherwise, the ghost eats the Pac-man. An animation of showing a dying Pac-Man appears at this moment. This is in fact a shrinking circle(Arc) which disappears at the end of the animation. The animation is accomplished by the DyingPacMan class.
The below figure depicts the animation process of the dying Pac-man character.

The code of DyingPacMan.fx is listed below:
/*
* DyingPacMan.fx
*
* Created on 2009-2-6, 17:52:42
*/
package pacman;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.shape.Arc;
/**
* @author Henry Zhang
*/
public class DyingPacMan extends Arc {
public var maze : Maze;
var timeline = Timeline {
repeatCount: 1
keyFrames: [
KeyFrame {
time: 600ms
action: function() {
// hide the pacMan character and ghosts before the animation
maze.pacMan.visible = false;
for ( g in maze.ghosts ) {
g.hide();
}
visible = true;
}
values: [ startAngle => 90, length=>360 ];
},
KeyFrame {
time: 1800ms
action: function() {
visible = false;
}
values: [ startAngle => 270 tween Interpolator.LINEAR,
length => 0 tween Interpolator.LINEAR ]
},
]
}
public function startAnimation(x: Number, y: Number) : Void {
startAngle = 90;
centerX = x;
centerY = y;
timeline.play();
}
}
Interpolations of two variables, startAngle and length, are involved during the animation. To better illustrate this process, the below figure shows the change of the shape against a timeline.
Hope you enjoy reading the articles. You can use arrow keys to play the current version of the game. The ghosts are moving randomly which makes the game less challenging. In the next article, I will introduced a better algorithm. Try it by clicking the below screenshot:
Related Articles:
Develop Games in JavaFX
My JavaFX Demo Game: Pac-Man
