2

I am using the Libgdx framework along with Spine to do Skeleton animation using Atlas and exported Json file. In the file there are many animations. I am able to load animations from the json file. But there are 2 issues :

  1. Lag when animation starts : there is a lag of 3-4 seconds before the animation starts. I see a black screen and then the animation works.

  2. Delta - i have some issues with the delta time. Only some animations are running from the default delta value, however if i change ( increment or decrease the delta value), different animations give different results.

Cant understand what to do ? I have searched thoroughly in the internet but haven't found the solution.

I am posting my code below:

This is my Main Class

public class AndroidLauncher extends AndroidApplication 

{

    SpriteBatch batch;
    Texture img;

    ShapeRenderer renderer;

    TextureAtlas atlas;
    Skeleton skeleton;
    Animation animation;
    float time;
    Bone root;
    float x,y;

    View spine_view;


    private Screen curScreen; // the current screen

    @Override
    protected void onCreate (Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        //initialize(new Main(20), config);
        spine_view=initializeForView(new ApplicationListener()
        {

            @Override
            public void resume()
            {
                curScreen.resume();

            }

            @Override
            public void resize(int width, int height)
            {


            }

            @Override
            public void render()
            {

                curScreen.render((Gdx.graphics.getDeltaTime()/(1.5f))); //call the rendermethod with the delta time as parameter


            }

            @Override
            public void pause()
            {
                curScreen.pause();

            }

            @Override
            public void dispose()
            {

                curScreen.dispose();

            }

            @Override
            public void create()
            {

                Gdx.graphics.getWidth();
                Gdx.graphics.getHeight();

                Firstscreen temp = new Firstscreen();//just an example of the first screen to load up
                setScreen(temp);

            }
        }, config);


        LinearLayout l1 = (LinearLayout) findViewById(R.id.container);
        l1.addView(spine_view);
    }

     public void setScreen(Screen s) {
            if (curScreen != null) {
                curScreen.hide();
                curScreen.dispose();
            }
            curScreen = s;
            curScreen.show();


        }

}

...............................................

And this is the screen Iam rendering

public class Firstscreen implements Screen
{

    SpriteBatch batch;
    Texture img;

    ShapeRenderer renderer;

    TextureAtlas atlas;
    Skeleton skeleton;
    Animation animation;
    float time;
    Bone root;
    float x,y;

    int count =0;
    @Override
    public void render(float delta)
    {

        // track

            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            time += delta;


            animation.apply(skeleton,delta,time, false, null);
            SkeletonRenderer render = new SkeletonRenderer();


            batch.begin();

            render.draw(batch, skeleton);
            skeleton.updateWorldTransform();

            batch.end();


    }

    @Override
    public void resize(int width, int height)
    {


    }

    @Override
    public void show()
    {

        batch = new SpriteBatch();

        renderer = new ShapeRenderer();

        atlas = new TextureAtlas(Gdx.files.internal("abc.atlas"));
        SkeletonJson json = new SkeletonJson(atlas);

        // set the scale of skeleton
        json.setScale(0.3f);
        SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal("skeleton.json"));
        skeleton = new Skeleton(skeletonData);
        skeleton.setToSetupPose();
        skeleton.setSkin("Carla");


        // set the position of the skeleton to render( here middle)

        skeleton.setPosition((Gdx.graphics.getWidth()/2)-(x/2),( Gdx.graphics.getHeight()/2)-(y/2));


        //animation = skeletonData.findAnimation("KID-LEVEL 2 - Couch potato");

         animation = skeletonData.findAnimation("LEVEL 91- Around a world");
        //animation = skeletonData.findAnimation("KID-LEVEL 7 - Super Hero");




    }

    @Override
    public void hide()
    {


    }

    @Override
    public void pause()
    {


    }

    @Override
    public void resume()
    {


    }

    @Override
    public void dispose()
    {
        batch.dispose();
    }

}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • I am not sure if this helps, but this is how I track time in LibGDX: http://stackoverflow.com/questions/23995854/how-to-track-time-in-libgdxandroid/23996041#23996041 – EpicPandaForce Jul 03 '14 at 12:53
  • my scenario is very different.. I have some preloaded animations in json... thanks for commenting – Gaurav Choudhary Jul 03 '14 at 12:58
  • I haven't used Spine with libgdx, but from the documentation of the Animation class, it looks like you're using this line wrong: `animation.apply(skeleton,delta,time, false, null);`. The second parameter should be the last time passed in, not the delta time, so use `animation.apply(skeleton,time-delta,time, false, null);` instead. – Tenfour04 Jul 03 '14 at 14:40
  • @Tenfour04.. I tried ur way.. but still I get the same results.. Lag at the start and improper animations. Thanks for suggesting. – Gaurav Choudhary Jul 04 '14 at 04:00

1 Answers1

0

That's happens beacuse you are managing a timeline manually.

Use AnimationState instead:

Skeleton skeleton = new Skeleton(skeletonData);
// sctruct for mix animations
AnimationStateData stateData = new AnimationStateData(skeletonData);
// and state
AnimationState state = new AnimationState(stateData);

In some update method (or render, if you prefer update while rendering) call something like this:

state.update(delta);

And in the actual render method:

// apply the state to skeleton
state.apply(skeleton);
// update transformations
skeleton.updateWorldTransform();
// draw then
render.draw(batch, skeleton);

So, AnimationState isn't designed not for managing the delta only, you can play&mix animations using it:

state.setAnimation(0, "LEVEL 91- Around a world", isLoop);

To change animation speed use setTimeScale:

state.setTimeScale(0.5f); // set any time scale you want

Also, you may have some perfomance issues because you calling new SkeletonRenderer() in render method. To fix that, move this code to constructor.

desertkun
  • 1,027
  • 10
  • 19