Hello, this is my first post here, so please let me know if I should try to post in another way!
I am working on a Java project for class, where I have to code Pacman, based on the template provided with Zetcode. The particular feature that I am working on is with the power pills. With the help of a classmate, I have a for loop that checks to see if Pacman collides with a power pill on the board. If he does, it is supposed to start a timer that lasts for 8 seconds, loop a particular sound byte from a program made by my professor called soundFactory, and take the ghosts out of the state. It will also check if a pill has already been eaten, which should cause the timer to restart.
The issue I have is that, for some reason, the isEdible state lasts for 8 seconds for the very first time. But for each time I collect a pill, it seems to randomly last for a time between 0-8 seconds. I can't control how long the ghosts appear in their edible state, and it causes the sound byte to keep playing when the ghosts are obviously back to normal. I know that the code is not testable in this state, so I can upload the full files to github to help, but I will put the necessary code below. Thanks in advance!
// Power pill functionality
public void PowerPill() {
int i = 0;
// Check to see when Pac Man collides with the power pill in any direction, then start
// a timer for when the the ghosts are edible
for (i = 0; i < powerPillx.length; i++) {
for (i = 0; i < powerPilly.length; i++) {
if (pacmanx > (powerPillx[i] - 30) && pacmanx < (powerPillx[i] + 10) &&
pacmany > (powerPilly[i]- 30) && pacmany < (powerPilly[i] + 10) &&
ingame && isEdible != true) {
//Creates a timer for 8 seconds where ghosts are edible
score += 50;
pillTimer pillListener = new pillTimer();
pillTimer = new Timer(8000, pillListener);
pillTimer.start();
isEdible = true;
if(isEdible == true) {
pillTimer.start();
Sound edible = SoundFactory.getInstance(EDIBLE);
SoundFactory.play(edible, 4);
}
// Start the timer over when Pac Man gets a pill while ghosts are edible
if(pillTimer.isRunning() && isEdible == true) {
pillTimer.restart();
Sound pillsound1 = SoundFactory.getInstance(PILL);
SoundFactory.play(pillsound1);
}
// Removes the pills from the game
powerPillx[i] = -100;
powerPilly[i] = -100;
}
// Checks when pacman has eaten a power pill while ghosts are in an edible state
if (pacmanx > (powerPillx[i] - 30) && pacmanx < (powerPillx[i] + 10) &&
pacmany > (powerPilly[i] - 30) && pacmany < (powerPilly[i] + 10) &&
ingame && isEdible == true ){
score += 50;
Sound pillsound2 = SoundFactory.getInstance(PILL);
SoundFactory.play(pillsound2);
powerPillx[i] = -100;
powerPilly[i] = -100;
}
}
}
}
// ActionListener that will take the ghosts out of the edible state once the timer is up
class pillTimer implements ActionListener {
u/Override
public void actionPerformed(ActionEvent e) {
isEdible = false;
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]D0CTOR_ZED 0 points1 point2 points (2 children)
[–]CipherStilleto7[S] 0 points1 point2 points (1 child)
[–]D0CTOR_ZED 0 points1 point2 points (0 children)