1

Как сделать так, чтоб при таком коде анимация включалась через определенное время(например каждые 10 секунд). Если есть возможность, приведите пример. Спасибо

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;



public class Animator implements ApplicationListener {

private static final int FRAME_COLS = 6;
private static final int FRAME_ROWS = 5; 

Animation walkAnimation; 
Texture walkSheet;
TextureRegion[] walkFrames;
SpriteBatch spriteBatch; 
TextureRegion currentFrame; 

float stateTime;

@Override
public void create() {
    walkSheet = new Texture(Gdx.files.internal("animation_sheet.png"));
    TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS);
    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);
    spriteBatch = new SpriteBatch();
    stateTime = 0f;
}

@Override
public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stateTime += Gdx.graphics.getDeltaTime();
    currentFrame = walkAnimation.getKeyFrame(stateTime, false);  //Цикличность отключена
    spriteBatch.begin();
    spriteBatch.draw(currentFrame, 50, 50);
    spriteBatch.end();
}
}

1 ответ 1

1

Аналогичный вопрос: Таймер в Java Swing
Пример оттуда:

import javax.swing.Timer; //Будет вызываться каждую секунду

timer = new Timer( 1000, new ActionListener(
    public void actionPerformed(ActionEvent ev) {
         System.out.println( "WOW!" );
    }
));

timer.start();

Или же другой пример оттуда:

public class Test {
    public static void main(String args[]) {
        new Thread(new Runnable() {
            public void run() {
                while(true) { //бесконечно крутим
                    try {
                        Thread.sleep(4000); // 4 секунды в милисекундах
                        System.out.println("Hi!");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

Думаю, как применить это в своём коде, вы разберётесь. Вот официальная документация: docs.oracle.com

2
  • Так я не разобрался с этим таймером, поэтому решил спросить..)
    – Rusl Mag
    6 мар 2017 в 14:56
  • Возьмите первый пример, и вместо System.out.println("Wow!"); добавьте ваше действие, оно будет выполняться каждые 1000 миллисекунд, то есть раз в секунду 6 мар 2017 в 14:57

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.