Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  (LibGdx) Controlling an animation  (Read 3553 times)
0 Members and 1 Guest are viewing this topic.
Offline Fairy Tailz

Senior Newbie





« Posted 2015-03-01 00:32:34 »

I'm trying to get an animation to start until it finished whenever I click the screen. I can't get it to stop looping.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
public class DungeonedFate implements ApplicationListener {
   
   boolean animateIt = false;
   
   
    private static final int        FRAME_COLS = 6;         // #1
    private static final int        FRAME_ROWS = 5;         // #2

    Animation                       walkAnimation;          // #3
    Texture                         walkSheet;              // #4
    TextureRegion[]                 walkFrames;             // #5
    SpriteBatch                     spriteBatch;            // #6
    TextureRegion                   currentFrame;           // #7

    float stateTime;                                        // #8

    @Override
    public void create() {
        walkSheet = new Texture(Gdx.files.internal("sprite-animation1.png")); // #9
        TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS);              // #10
        walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }
        walkAnimation = new Animation(0.025f, walkFrames);      // #11
        spriteBatch = new SpriteBatch();                // #12
        stateTime = 0f;                         // #13
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);                        // #14
        stateTime += Gdx.graphics.getDeltaTime();           // #15
        currentFrame = walkAnimation.getKeyFrame(stateTime, animateIt);  // #16
        spriteBatch.begin();
        spriteBatch.draw(currentFrame, 50, 50);             // #17
        spriteBatch.end();
       
        checkClick();                               ////////////////////
    }
   
    public void checkClick() {
       if(Gdx.input.isButtonPressed(0)) {
          animateIt = true;
         
          System.out.println("Start you animation!");
       }
    }


Click to Play


as you can see it loops once, then when my (invisible) mouse clicks the screen; it restarts and never stops. This is as close as I could get after failing miserably multiple times.
Offline atombrot
« Reply #1 - Posted 2015-03-01 02:33:40 »

If I'm not mistaken the second parameter of getKeyFrame is the looping parameter. Once you click animateIt it is set to true, hence your animation starts to looping.

Change following things:
- Only update state time when animateIt is true.
- Use false as second parameter in getKeyFrame
- Reset the state time to 0 when the mouse has been clicked.

With this the image will be the first key frame of the animation, until you click. When you click the animation plays and should stop at the last key frame. When the user clicks again, it starts from beginning.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
@Override
public void render() {
   Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);                        // #14
+   if(animateIt){
      stateTime += Gdx.graphics.getDeltaTime();           // #15
+   }
-   currentFrame = walkAnimation.getKeyFrame(stateTime, animateIt);  // #16
+   currentFrame = walkAnimation.getKeyFrame(stateTime, false);  // #16
   spriteBatch.begin();
   spriteBatch.draw(currentFrame, 50, 50);             // #17
   spriteBatch.end();
           
   checkClick();                               ////////////////////
}

public void checkClick() {
   if(Gdx.input.isButtonPressed(0)) {
      animateIt = true;
+      stateTime = 0f;                                
      System.out.println("Start you animation!");
   }
}
   



It would probably be good to check if the animation has been completed and set animateIt to false, if so.
Offline Fairy Tailz

Senior Newbie





« Reply #2 - Posted 2015-03-01 03:27:21 »

Thanks, it worked perfectly. I can't medal you for some reason sadly.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ziozio
« Reply #3 - Posted 2015-03-01 08:38:48 »

Thanks, it worked perfectly. I can't medal you for some reason sadly.

I appreciated on your behalf Smiley
Offline atombrot
« Reply #4 - Posted 2015-03-01 12:25:41 »

Thanks, it worked perfectly. I can't medal you for some reason sadly.

A thanks is fine. Glad it worked.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (201 views)
2019-09-04 15:33:17

hadezbladez (4922 views)
2018-11-16 13:46:03

hadezbladez (1815 views)
2018-11-16 13:41:33

hadezbladez (5196 views)
2018-11-16 13:35:35

hadezbladez (1025 views)
2018-11-16 13:32:03

EgonOlsen (4397 views)
2018-06-10 19:43:48

EgonOlsen (5230 views)
2018-06-10 19:43:44

EgonOlsen (2973 views)
2018-06-10 19:43:20

DesertCoockie (3876 views)
2018-05-13 18:23:11

nelsongames (4308 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!