I have a very simple program where you can use your W, A, S, D and space keys to shoot. All of the shooting and movin
In general, when you must "constantly observe" something in an OO language, the best solution is to use the Observer pattern.
You can check if the bullet
intersects other
using Shape.intersect() in a custom Interpolator supplied to each relevant KeyValue. The fragment below adds an Interpolator
to shoot()
, but you'll need an identical one for each direction. The implementation is linear, simply returning t
unchanged, but you might also look at this parabolic interpolator. I also made other
a class variable, accessible to shoot()
. I put a dozen bullets into the air with no perceptible delay. Note that you don't need a start
value: "one will be synthesized using the target values that are current at the time" the animation is played.
private Circle other = new Circle(100, 100, 10);
…
else {
KeyValue end = new KeyValue(bullet.translateYProperty(), -800, new Interpolator() {
@Override
protected double curve(double t) {
if (!Shape.intersect(bullet, other).getBoundsInLocal().isEmpty()) {
System.out.println("Intersection");
}
return t;
}
});
KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
timeline.getKeyFrames().addAll(endF);
}
What you need is something that will check for collision every time you draw a frame in your game. A timer would run in a separate thread which could work, but would be much harder to synchronize with the game.