简体   繁体   中英

Textures to texture atlas and animation

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;
            }
        }

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM