This topic is 2030 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

This animation code draws a specific animation frame depending on which y coordinate the monster is on.
My bulky animation code was condensed into the code below by a forum user on the gamedev forum.
I never understood why the break statement is needed. I decided to comment the break statement out and notice it will only draw the last animation frame. If I have the break statement, it will instead animate all frames.

I know a break statement breaks out of the closest loop it is close to. But it does not give me an insight on why the break statement is needed had I not run the code experimentally.

// 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;
}
}

Edited by warnexus

Share on other sites
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.

Share on other sites

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?

Edited by warnexus

Share on other sites

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);



Share on other sites

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!

Share on other sites

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! -_-

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.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633735
• Total Posts
3013596
×