Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

Question about animation code

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!