Продолжаем делать игру Flappy Bird для android. В этом уроке научим нашу птичку махать крыльями в полете — добавим в игру анимацию.
Скачать графические ресурсы для игры
Исходный код измененных классов под видео:
package info.fandroid.game.sprites;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
/**
* Created by Vitaly on 22.12.2015.
*/
public class Animation {
private Array<TextureRegion> frames;
private float maxFrameTime;
private float currentFrameTime;
private int frameCount;
private int frame;
public Animation(TextureRegion region, int frameCount, float cycleTime){
frames = new Array<TextureRegion>();
int frameWidth = region.getRegionWidth() / frameCount;
for (int i = 0; i < frameCount; i++){
frames.add(new TextureRegion(region, i * frameWidth, 0, frameWidth, region.getRegionHeight()));
}
this.frameCount = frameCount;
maxFrameTime = cycleTime / frameCount;
frame = 0;
}
public void update(float dt){
currentFrameTime += dt;
if (currentFrameTime > maxFrameTime){
frame++;
currentFrameTime = 0;
}
if (frame >= frameCount)
frame = 0;
}
public TextureRegion getFrame(){
return frames.get(frame);
}
}
package info.fandroid.game.sprites;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
/**
* Created by Vitaly on 06.11.2015.
*/
public class Bird {
private static final int MOVEMENT = 100;
private static final int GRAVITY = -15;
private Vector3 position;
private Vector3 velosity;
private Rectangle bounds;
private Animation birdAnimation;
private Texture texture;
public Bird(int x, int y){
position = new Vector3(x, y, 0);
velosity = new Vector3(0, 0, 0);
texture = new Texture("birdanimation.png");
birdAnimation = new Animation(new TextureRegion(texture), 3, 0.5f);
bounds = new Rectangle(x, y, texture.getWidth() /3, texture.getHeight());
}
public Vector3 getPosition() {
return position;
}
public TextureRegion getBird() {
return birdAnimation.getFrame();
}
public void update(float dt){
birdAnimation.update(dt);
if (position.y > 0)
velosity.add(0, GRAVITY, 0);
velosity.scl(dt);
position.add(MOVEMENT * dt, velosity.y, 0);
if (position.y < 0)
position.y = 0;
velosity.scl(1 / dt);
bounds.setPosition(position.x, position.y);
}
public void jump(){
velosity.y = 250;
}
public Rectangle getBounds(){
return bounds;
}
public void dispose() {
texture.dispose();
}
}
Больше уроков:
Уроки Android Studio: тут
Инструменты android разработчика: тут
Дизайн android приложений: тут
Уроки создания игр для android: тут
Основы программирования на JAVA: тут
Урок 10. Flappy Bird: добавляем текстуру земли и оптимизируем код для запуска игры на Android
Урок 12. Flappy Bird: добавляем в игру звуки и экран Game Over| Делаем android игры на LibGDX