Editing initTargets function - unsubscribe&resubscribe

This commit is contained in:
Yewon Kim 2025-05-18 23:20:32 +09:00
parent 79b023829f
commit 9c9865f149

View File

@ -54,9 +54,9 @@ function keyPressed() {
// reset score and targets
gun.remainingShots = TOT_SHOTS;
score.shotLeft = TOT_SHOTS;
score.score = 0;
initTargets();
}
}
@ -65,8 +65,14 @@ function initTargets() {
// Create new targets from the factory
// Remember to unsubscribe the previous targets and to subscribe the new ones
const factory = TargetFactory.getInstance();
gun.unsubscribeAll();
targets = factory.getRandomTargets(MAX_TARGETS, TARGET_WIDTH, 200);
gun.subscribe(score);
for (let target of targets) {
gun.subscribe(target);
target.subscribe(score);
}
}
// Do not touch these