All the Interesting ThingsThis is a personal web log. All information posted here does not represent my employer. I do not speak for my employer. |
|
Friday Jun 05, 2009
Latest Article on Writing the JavaFX Pac-Man Game
My latest article of a series, "Writing the Pac-Man Game in JavaFX Part 4", was out on June 4. The articles are getting more and more interesting now. In this article, the interaction between Pac-Man character and the ghosts was introduced in details. The article showed how to determine whether the Pac-man character and a ghost touched each other. A simplified equation 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, at this moment, an animation of showing a dying Pac-Man appears. This is in fact a shrinking circle 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.
public class DyingPacMan extends Arc {
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 ]
},
]
}
... code omitted ...
}
As you can see it in the code, there are two keyframes of the animation. Interpolations of two instance 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: Posted at 11:48PM Jun 05, 2009 by morningstar in JavaFX | |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||