纹理到纹理图集和动画

时间:2017-03-08 10:35:12

标签: java android animation libgdx texture-atlas

我认为在我的动画中加载许多纹理会导致游戏中的轻微延迟,并且我希望我的数组纹理在TextureAtlas中。下面的代码是我的动画代码,它还将获得动画中的纹理帧索引,并在显示纹理索引时执行任务....,我已经有一个名为shot.pack的.pack文件,用于纹理图集但是我我不知道如何应用它并使它像这样。我希望有人可以提供帮助

     Texture[] shot;
     //At CONSTRUCTOR
    shot = new Texture[21];
    for(int i=0; i <21; i++){
        if(i == 19 || i==20 ){
            shot[i]=new Texture("s18.png");
        }
        else {
            shot[i] = new Texture("s" + Integer.toString(i) + ".png");
        }

    }
        //animation
    animation = new Animation(1/19f,shot);


   //@render METHOD
   sb.draw((Texture) animation.getKeyFrame(timePassed, false), ShootingTreys.WIDTH * 0.03f, ShootingTreys.HEIGHT * 0.03f, (ShootingTreys.WIDTH * 1 / 6), (ShootingTreys.HEIGHT / 2) + (ShootingTreys.HEIGHT * 0.13f));

        if (animation.getKeyFrameIndex(timePassed) == 20) {
            isShotDelay = true;
            shotDelay += Gdx.graphics.getDeltaTime();

            if (shotDelay >= 0.2f || ball.getBall().getY() < ShootingTreys.HEIGHT * 0.2f) {
                touch = false;
                timePassed = 0;
                shotDelay = 0;
                isShotDelay = true;
                //for missed ball
                colide = false;
            }
        }

1 个答案:

答案 0 :(得分:0)

将所有动画帧保存在一个单独的.pack文件中

    TextureAtlas aniAtlas=new TextureAtlas("ani.pack");
    Array<TextureAtlas.AtlasRegion> animationFrame=aniAtlas.getRegions();
    float duration=.4f;
    Animation<TextureRegion> animation=new Animation<TextureRegion>(duration,animationFrame);

一个.pack中的多个动画文件 保持相似类型文件的索引概念,例如。
bird_0.png,bird_1.png,.....
cat_0.png,cat_1.png,.....

    TextureAtlas multiAniAtlas=new TextureAtlas("ani.pack");
    Array<TextureAtlas.AtlasRegion> birdFrames=multiAniAtlas.findRegions("bird");
    float duration1=.55f;
    Animation<TextureRegion> birdAnimation=new Animation<TextureRegion>(duration1,birdFrames);

使用可以使用AssetManger并以这种方式加载您的TextureAtlas:

    AssetManager assetManager=new AssetManager();
    assetManager.setLoader(TextureAtlas.class, new TextureAtlasLoader(new InternalFileHandleResolver()));
    assetManager.load("ani.pack", TextureAtlas.class);
    assetManager.finishLoading();

    TextureAtlas atlas=assetManager.get("ani.pack");
相关问题