Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Question about animation code


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1472

Like
0Likes
Like

Posted 24 May 2013 - 07:59 PM

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[i])
{
g.drawImage(ghostMovementAnimation.get(i), (int)position.getX(), (int)position.getY(), null);
break;
}
}

Edited by warnexus, 24 May 2013 - 08:03 PM.


Sponsor:

#2 frob   Moderators   -  Reputation: 22242

Like
0Likes
Like

Posted 24 May 2013 - 08:47 PM

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#3 warnexus   Prime Members   -  Reputation: 1472

Like
0Likes
Like

Posted 24 May 2013 - 09:21 PM

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, 24 May 2013 - 09:23 PM.


#4 Khatharr   Crossbones+   -  Reputation: 3031

Like
1Likes
Like

Posted 24 May 2013 - 09:40 PM

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[i]) {
    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.

#5 warnexus   Prime Members   -  Reputation: 1472

Like
0Likes
Like

Posted 24 May 2013 - 09:59 PM

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[i]) {
    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



#6 frob   Moderators   -  Reputation: 22242

Like
1Likes
Like

Posted 25 May 2013 - 04:59 PM

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[i]) {
    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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#7 Khatharr   Crossbones+   -  Reputation: 3031

Like
0Likes
Like

Posted 25 May 2013 - 09:39 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS