Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Animation Example  (Read 2018 times)

0 Members and 1 Guest are viewing this topic.

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Animation Example
« on: May 19, 2017, 08:21:32 pm »
Hello! I want to make a game(a Platformer exactly) and I want to implement animated movement with a spritesheet, don't tell me to google it because I did it and didn't found what I want. I want a exact code( not a full code, a formula of it) and I know there's a lot of tutorials on Youtube, but it isn't the exact thing what I want.
Guess Who's Back ?

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Animation Example
« Reply #1 on: May 19, 2017, 10:37:42 pm »
Hello

What you would have to do (the easiest way) is to store the image (the sprite sheet) to a Texture, then supposedly you would have and index into your main character's draw method or function that you pass to the vector or matrix which contains the data of each rectangle that contains every sub picture that your character may display at each frame. Your character, so as the other STAFF in your game may be in different attitudes, such as walking, jumping, swimming, dying, etc. That logic in your code will decide, appart from the location on the screen and other circumstances, which of all the sub pictures from the character's sprite sheet will be drawn at each frame.

You would need to open your sprite sheet file in your picture Editor, and see for every sub picture in the sheet, its left, top, width, and height.

Then you can have a matrix like this in your character class:

int matrix[][] = new int[][] { new int[] { x1, y1, w1, h1 }, new int[] { x2, y2, w2, h2}, ... };

with so many vectors as subpictures are in your sprite sheet

And into your method or function (texture contains your sprite sheet):

Sprite hero = new Sprite(texture);
hero.TextureRect = new IntRect(matrix[index][0], matrix[index][1], matrix[index][2], matrix[index][3]);
hero.Position = new Vector2f(yourX, yourY);
window.Draw(hero);

Maybe your index (int) variable could be from 1 to 4 while the hero is walking (this means 4 sub pictures of the hero walking), 0 while it is stopped, 5 when it jumps, etc, depending on your input, or other cases such as if you are beaten, or you collide with a wall or a ceiling.

You can have a variable for the state (walking, jumping, etc), and another for the progress of each of them. For example, state = States.Walk and step = 1 (to 4) for each of the sub pictures that shape the walking animation

if (state == States.Stop)
{
    index = 0;
}
else if (state == States.Walk)
{
    index = step / 5 + 1;    // + 1 because it goes from 1 to 4, remember 0 is the index for stopped
    step++;
    if (step == 20)
        step = 0;
}

// before, when you (suppose) "land"

state = States.Walk;
step = 0;

You can scan in my poor Super Mario source code the logic for animation. (I hope, because it's not the most tidy in the world)
https://www.dropbox.com/s/hfkxetoxs5iv10i/My%20New%20Super%20Mario%20Bros%20Folder.rar?dl=0

Please let me know if you progress or if you have any more doubts


Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Animation Example
« Reply #2 on: May 20, 2017, 08:44:30 am »
I can't open your Super Mario code...Can't you give me the exact formula for it? I mean it's like you explaining me what does cout, instead of showing me the "cout<<""<<endl;", not the best explanation, I know,but I think you know what I mean...
Guess Who's Back ?

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: Animation Example
« Reply #3 on: May 20, 2017, 12:23:18 pm »
Nevermind...I found a tutorial(more exactly David Dolynny's), it's exactly what I wanted. Sorry for the trouble :-[ , thanks for your help Tigre Pablito.
Guess Who's Back ?

 

anything