我想在用户触摸屏幕时创建踢人动画。我这样从render(){}
调用一个函数
if(Gdx.input.isTouched()) {
if(lr.contains(Gdx.input.getX(), Gdx.input.getY())){
//start animation 1-2-3-3-2-1 complete kick
myAnimation(); //this is my function. code below,
batch.begin();
sprite.draw(batch);
batch.end();
}
}
myAnimation()
是我创建新任务的地方。了解我保持间隔5秒的每个动作。myAnimation()
private void myAnimation(){
Timer.schedule(new Task() {
@Override
public void run() {
// TODO Auto-generated method stub
if(positiveCycle){
currentFrame++;
if(currentFrame>3) {
positiveCycle = false;
currentFrame = 3;
}
}else{
currentFrame--;
if(currentFrame==0){
//get out of this loop
currentFrame = 1;
positiveCycle = true;
this.cancel();
}
}
currentAtlasKey = String.format("%04d", currentFrame);
sprite.setRegion(textureAtlas.findRegion(currentAtlasKey));
}
}
}
但是,当我触摸屏幕时,在5秒间隔后我看不到每个臭虫,它非常快地显示了臭虫。不到20毫秒。
完整的代码
package com.geek.tanvir.KickingGame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
//import com.badlogic.gdx.graphics.GLTexture;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
//import com.badlogic.gdx.graphics.Texture.TextureFilter;
//import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
//import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
public class KickingGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texturePlayer;
private Texture texturePlayerKick;
private Texture texturePlayerKick2;
private Texture texturePlayer2;
private Texture ball;
private TextureAtlas textureAtlas;
private Sprite sprite;
private int currentFrame = 1;
private String currentAtlasKey = new String("0001");
int currentframe = 1;
private boolean positiveCycle = true;
//private Sprite sprite;
private Rectangle lr;
private Rectangle rr;
@Override
public void create() {
//float w = Gdx.graphics.getWidth();
//float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
Texture.setEnforcePotImages(false);
texturePlayer = new Texture(Gdx.files.internal("player.jpg"));
texturePlayerKick = new Texture(Gdx.files.internal("playerKick.jpg"));
texturePlayer2 = new Texture(Gdx.files.internal("player2.jpg"));
texturePlayerKick2 = new Texture(Gdx.files.internal("playerKick2.jpg"));
ball = new Texture(Gdx.files.internal("ball.jpg"));
lr = new Rectangle();
rr = new Rectangle();
textureAtlas = new TextureAtlas(Gdx.files.internal("sheet.atlas"));
AtlasRegion region = textureAtlas.findRegion("0001");
sprite = new Sprite(region);
sprite.setPosition(120, 100);
sprite.scale(0.02f);
lr.set(0, 0, 400, 400);
rr.set(400, 0, 400, 400);
}
@Override
public void dispose() {
batch.dispose();
texturePlayer.dispose();
texturePlayer2.dispose();
texturePlayerKick.dispose();
texturePlayerKick2.dispose();
ball.dispose();
}
private void myAnimation(){
Timer.schedule(new Task() {
@Override
public void run() {
// TODO Auto-generated method stub
if(positiveCycle){
currentFrame++;
if(currentFrame>3) {
positiveCycle = false;
currentFrame = 3;
}
}else{
currentFrame--;
if(currentFrame==0){
//get out of this loop
currentFrame = 1;
positiveCycle = true;
this.cancel();
}
}
currentAtlasKey = String.format("%04d", currentFrame);
sprite.setRegion(textureAtlas.findRegion(currentAtlasKey));
}
}, 5, 5);
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texturePlayer, 300-texturePlayer.getWidth(),100);
batch.draw(texturePlayer2, 500, 100);
batch.draw(ball, 400-ball.getWidth(),500-ball.getHeight());
batch.end();
if(Gdx.input.isTouched()) {
if(lr.contains(Gdx.input.getX(), Gdx.input.getY())){
//start animation 1-2-3-3-2-1 complete kick
myAnimation();
batch.begin();
sprite.draw(batch);
batch.end();
}
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
最佳答案
我想您想显示一个Sprite
,直到用户触摸为止。然后,它会生成Animation
,如果已完成,它将再次处于静止状态,直到用户再次触摸正确吗?
为此,使用Animation并使用其第二个构造函数:
Animation myAnim = new Animation(frameDuration, animationTextures, PlayMode.Normal);
然后存储
boolean animating = false;
和float stateTime = 0;
如果用户触摸您,请设置
animating = true;
在渲染中说:
if (animating) {
stateTime+=delta;
TextureRegion currentFrame = myAnim.getKeyFrame(stateTime);
// draw the currentFrame
if(myAnim. isAnimationFinished(stateTime)) {
animating = false;
stateTime = 0;
}
}
else {
// draw the unanimated Frame;
}
阅读内容:Animation API
Tutorial on GitHub