我的舞台不播放精灵动画(Libgdx)

时间:2017-05-10 23:54:28

标签: java android libgdx

我正在尝试为我的场景中的某些演员重现精灵动画。除了阅读一本书之外,我还在观看有关如何使用“FlappyBird”游戏libgdx创建的分步教程。这并不是说我做同样的事情要么比视频中的人想要做的那样。

我试图制作像“水果粉碎”或“宝石迷阵”这样的游戏......就像那样。我创建了我的菜单和我的游戏(或者像教程一样的播放状态)。我也创建了一个表类(这个类扩展了Stage)来管理舞台的框(演员)。程序已经将盒子放在各自的位置并获得每个盒子的坐标,这样当你点击一个盒子时,游戏就知道它是哪个盒子。

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

My 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 个答案:

答案 0 :(得分:0)

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

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

其中4是最大值,1是最小值,将来可能是你的变量。

我希望它有所帮助! Saludos atodoPanamá!