gpt4 book ai didi

java - 我的舞台不播放 Sprite 动画 (Libgdx)

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:58 25 4
gpt4 key购买 nike

我正在尝试为我的场景中的某些 Actor 重现 Sprite 动画。除了看书之外,我还观看了如何使用 libgdx 创建“FlappyBird”游戏的分步教程。这并不是说我正在做与视频中的人想做的事情相同的事情。

我正在尝试制作一款类似“水果粉碎”或“ gem 迷阵”之类的游戏。我创建我的菜单和我的游戏(或像教程一样的游戏状态)。我还创建了一个表类(此类扩展了 Stage)来管理舞台的框( Actor )。程序已经将盒子放置在各自的位置,并获取每个盒子的坐标,这样当你点击一个盒子时,游戏就知道它是哪个盒子。

现在...我想重现一些盒子的 Sprite 动画...但我的 table (或舞台)不播放动画。

我的 Box 类是这样的:

public class Box extends Actor {
private Tipo tipo;
private boolean activo;
private String clase;
private TextureRegion region;
private float elapsedTime;
private Animation<TextureRegion> animacion,selected, unselected;
private Rectangle area;
private float velocidadY,velocidadX;
private int minY,minX,maxY,maxX;

public Box(){
super();
velocidadX = 0;
velocidadY = 0;
minY = 0;
minX = 0;
maxY = 0;
maxX = 0;
elapsedTime = 0;
activo = false;
region = new TextureRegion();
area = new Rectangle();
asignarTipo();
}

//HERE I ASSIGN THE TYPE OF EACH BOX
public void asignarTipo(){
Random rand = new Random();
int t = rand.nextInt((4-1)+1)+1;;
switch (t){
case 1:
tipo = Tipo.cbV2;
clase = "V2";
unselected = new Animation(0.2f,cargarSprites("1B",".png",1));
selected = new Animation(0.2f,cargarSprites("1",".png",2),Animation.PlayMode.LOOP_PINGPONG);
setAnimation(selected);
break;
case 2:
tipo = tipo.cbV4;
clase = "V4";
unselected = new Animation(0.2f,cargarSprites("2B",".png",1));
selected = new Animation(0.2f,cargarSprites("2",".png",2),Animation.PlayMode.LOOP_PINGPONG);
setAnimation(unselected);
break;
case 3:
tipo = tipo.cbV8;
clase = "V8";
unselected = new Animation(0.2f,cargarSprites("3B",".png",1));
selected = new Animation(0.2f,cargarSprites("3",".png",2),Animation.PlayMode.LOOP_PINGPONG);
setAnimation(unselected);
break;
case 4:
tipo = tipo.cbV16;
clase = "V16";
unselected = new Animation(0.2f,cargarSprites("4B",".png",1));
selected = new Animation(0.2f,cargarSprites("4",".png",2),Animation.PlayMode.LOOP_PINGPONG);
setAnimation(unselected);
break;
}
}

//setTexture
public void setTextura(Texture t){
int ancho = t.getWidth();
int alto = t.getHeight();
setWidth(ancho);
setHeight(alto);
region.setRegion(t);
}

//This function returns the sprite array
public Array<TextureRegion> cargarSprites(String imagen, String ext, int index){
TextureRegion frames[] = new TextureRegion[index];
for (int i = 0; i < index; i++){
if(imagen.length() < 2){
Texture t = new Texture(Gdx.files.internal(imagen + i + ext));
t.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
frames[i] = new TextureRegion(t);
}else{
Texture t = new Texture(Gdx.files.internal(imagen + ext));
t.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
frames[i] = new TextureRegion(t);
}
}
Array<TextureRegion> framesArray = new Array(frames);
return framesArray;
}

public void setAnimation(Animation<TextureRegion> a)
{
setTextura(a.getKeyFrame(0).getTexture());
animacion = a;
}

public Rectangle getBoundingRectangle(){
area.set(getX(),getY(),getWidth(),getHeight());
return area;
}

//Act method (or UPDATE)
public void Act(float dt){
super.act(dt);
elapsedTime += dt;
moveBy(velocidadX * dt, velocidadY * dt);
}

public void draw(Batch batch, float parentAlpha){
Color c = getColor();
batch.setColor(c.r, c.g, c.b, c.a);
region.setRegion(animacion.getKeyFrame(elapsedTime));
if ( isVisible() )
batch.draw( region, getX(), getY(), getOriginX(), getOriginY(),
getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation() );
}
}

表类:

 //I do a loop to render all the boxes of my table
public void updateTableObjects(float dt){
for(int f=0;f<baterias.length;f++){
for(int c=0;c<baterias[f].length;c++){
baterias[f][c].act(dt);
}
}
}

PlayState 类:

public class PlayState extends Estado {
private Texture background;
private Tabla tabla;
protected PlayState(GameStateManager gsm) {
super(gsm);
tabla = new Tabla();
background = new Texture("fondo_juego.png");
camara.setToOrtho(true,background.getWidth(),background.getHeight());
Image fondo = new Image(background);
}

@Override
public void update(float dt) {
handleInput();
}

@Override
public void handleInput() {
if(Gdx.input.isTouched()){
raton.set(Gdx.input.getX(), Gdx.input.getY(),0);
camara.unproject(raton);
if(raton.x > 173 && raton.y < 703)
tabla.buscarCubo(raton.x,raton.y);
System.out.println((int)raton.x + "," + (int)raton.y);
}
}

//I THINK HERE IS THE PROBLEM... I TRY TO PLACE THE METHOD THAT UPDATES
//ALL THE BOXES OF THE TABLE, BUT DIDNT WORK
@Override
public void render(SpriteBatch batch) {
batch.begin();
batch.draw(background,0,0, game.ANCHO, game.ALTO);
batch.end();
tabla.act(Gdx.graphics.getDeltaTime());
tabla.updateTableObjects(Gdx.graphics.getDeltaTime());
tabla.draw();
}
}

这里出了什么问题?

最佳答案

我想,也许,长时间查看你的代码,你应该尝试获取随机数的方式,因为你想要一个 1-4 之间的数字,要进行切换,试试这个 Hermano:

Random rn = new Random();
int n = 4 - 1 + 1;
int i = rn.nextInt() % n;
int randomNum = 1 + i;

其中4是最大,1是最小,将来也许会成为你的变量。

希望对您有帮助!向巴拿马致敬!

关于java - 我的舞台不播放 Sprite 动画 (Libgdx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43904381/

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