gpt4 book ai didi

java - 我如何在触摸时启动动画,该动画将在到达 Sprite 表末尾后停止?

转载 作者:行者123 更新时间:2023-12-01 13:14:12 28 4
gpt4 key购买 nike

我想在用户触摸屏幕时创建踢动画。我像这样从 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() 是我创建新任务的地方。为了理解每个 Action ,我保持了 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秒间隔后我看不到每个恶意内容,它的显示 Sprite 速度非常快。大约不到 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 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

关于java - 我如何在触摸时启动动画,该动画将在到达 Sprite 表末尾后停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22591324/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com