// animating the image frames of the ghost
for(int i = 0; i < 3;i++)
{
if(position.getY() < positionLimits)
{
g.drawImage(ghostMovementAnimation.get(i), (int)position.getX(), (int)position.getY(), null);
break;
}
}
Question about animation code
Here's how I would interpret the code:
Go through three images. If the position is less then the limit, draw the image and be done.
Without the break:
Go through three images. If the position is less then the limit, draw the image.
Without the break it will draw any values that satisfy the condition, and then keep going to draw any other images that also satisfy the condition.
The break is what makes it stop as soon as an image is successfully drawn.
The loop goes from 0 to 3. Really it should go to the number of animations rather than a hard-coded number.
Here's how I would interpret the code:
Go through three images. If the position is less then the limit, draw the image and be done.
Without the break:
Go through three images. If the position is less then the limit, draw the image.
Without the break it will draw any values that satisfy the condition, and then keep going to draw any other images that also satisfy the condition.
The break is what makes it stop as soon as an image is successfully drawn.
Without the break, it was suppose to drawn the corresponding i of the animation frame but it does not show that on the game window which is weird.
So the break stops the drawing of that image once the image has been drawn?
Break steps out of the for loop.
It may be clearer to you like this:
int i = 0;
while(i < 3) {
if(position.getY() < positionLimits) {
break;
}
else {
i++;
}
}
g.drawImage(ghostMovementAnimation.get(i), (int)position.getX(), (int)position.getY(), null);
Break steps out of the for loop.
It may be clearer to you like this:
int i = 0; while(i < 3) { if(position.getY() < positionLimits) { break; } else { i++; } } g.drawImage(ghostMovementAnimation.get(i), (int)position.getX(), (int)position.getY(), null);
Oh!!! It definitely makes it so much clearer! Now I get it! Thanks so much!
Oh!!! It definitely makes it so much clearer! Now I get it! Thanks so much! -_-Break steps out of the for loop.
It may be clearer to you like this:
int i = 0; while(i < 3) { if(position.getY() < positionLimits) { break; } else { i++; } } g.drawImage(ghostMovementAnimation.get(i), (int)position.getX(), (int)position.getY(), null);
Just note that the code above has an important difference.
In the original code it is possible that none of the images will be drawn if none of them satisfy the condition.
In the loop shown here, one if the images will always be drawn even if none of them satisfy the condition.