0

I think the loading of many textures in my animation cause the slight lag in my game, and i want my array textures to in a TextureAtlas. The code below is my animation code and also it will get the Texture frame index in the animation and perform a task if the texture index is shown...., I already have a .pack file named shot.pack for texture atlas but i didn't know how to apply it and make it like this. I hope someone can help

     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;
            }
        }
paul natividad
  • 161
  • 1
  • 1
  • 6

1 Answers1

0

Keep all frame of animation in one Separate .pack file

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

Multiple Animation's file in one .pack keep indexing concept for similar type of file like eg.
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);

Use can use AssetManger and load your TextureAtlas in this way :

    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");
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • How can i display a single frame if the animation is done? – paul natividad Mar 10 '17 at 05:05
  • My texture in texture packer was not in order – paul natividad Mar 10 '17 at 05:29
  • put a condition in rendering for animation and show single frame over that condition. – Abhishek Aryan Mar 10 '17 at 05:49
  • keep in order by indexing like for 1st frame give name xyz_0.png, for 2nd xyz_1.png and so on. – Abhishek Aryan Mar 10 '17 at 05:51
  • for more clarification check this https://github.com/libgdx/libgdx/wiki/Texture-packer#image-indexes – Abhishek Aryan Mar 10 '17 at 05:53
  • how to play an animation in this code? ` 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");` ,....thank you – paul natividad Mar 11 '17 at 14:27
  • TextureAtlas atlas=assetManager.get("ani.pack"); from this statement you got TextureAtlas from AssetManager then get Array of Regions from atlas. furthermore create Animation from Array of Region and play animation. – Abhishek Aryan Mar 11 '17 at 17:03