Question about animation code

Started by
5 comments, last by Khatharr 10 years, 11 months ago
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;
}
}
Advertisement
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.

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

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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! sleep.png

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.

Ah. True. My bad.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement