1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.GL20; 21 import com.badlogic.gdx.graphics.Texture; 22 import com.badlogic.gdx.graphics.g2d.Animation; 23 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 24 import com.badlogic.gdx.graphics.g2d.TextureRegion; 25 import com.badlogic.gdx.math.Vector2; 26 import com.badlogic.gdx.tests.utils.GdxTest; 27 28 public class SimpleAnimationTest extends GdxTest { 29 private Animation currentWalk; 30 private float currentFrameTime; 31 private Vector2 position; 32 33 private Texture texture; 34 35 private Animation downWalk; 36 private Animation leftWalk; 37 private Animation rightWalk; 38 private Animation upWalk; 39 40 private SpriteBatch spriteBatch; 41 42 private static final float ANIMATION_SPEED = 0.2f; 43 44 @Override create()45 public void create () { 46 Gdx.input.setInputProcessor(this); 47 texture = new Texture(Gdx.files.internal("data/animation.png")); 48 TextureRegion[][] regions = TextureRegion.split(texture, 32, 48); 49 TextureRegion[] downWalkReg = regions[0]; 50 TextureRegion[] leftWalkReg = regions[1]; 51 TextureRegion[] rightWalkReg = regions[2]; 52 TextureRegion[] upWalkReg = regions[3]; 53 downWalk = new Animation(ANIMATION_SPEED, downWalkReg); 54 leftWalk = new Animation(ANIMATION_SPEED, leftWalkReg); 55 rightWalk = new Animation(ANIMATION_SPEED, rightWalkReg); 56 upWalk = new Animation(ANIMATION_SPEED, upWalkReg); 57 58 currentWalk = leftWalk; 59 currentFrameTime = 0.0f; 60 61 spriteBatch = new SpriteBatch(); 62 position = new Vector2(); 63 } 64 65 @Override render()66 public void render () { 67 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 68 currentFrameTime += Gdx.graphics.getDeltaTime(); 69 70 spriteBatch.begin(); 71 TextureRegion frame = currentWalk.getKeyFrame(currentFrameTime, true); 72 spriteBatch.draw(frame, position.x, position.y); 73 spriteBatch.end(); 74 } 75 76 @Override touchDown(int x, int y, int pointer, int button)77 public boolean touchDown (int x, int y, int pointer, int button) { 78 position.x = x; 79 position.y = Gdx.graphics.getHeight() - y; 80 return true; 81 } 82 83 @Override dispose()84 public void dispose () { 85 spriteBatch.dispose(); 86 texture.dispose(); 87 } 88 } 89