Multiplayer animation in Libgdx

How do I get flip card animations in Libgdx (flip at some angle) - I used sprite.flip (boolean x, boolean y) , but could not achieve the desired result.

I want to do something like this:

http://developer.android.com/training/animation/cardflip.html

+4
source share
2 answers

If you use Actor in your code, you can use these two actions that I wrote:

public class MyActions {
    public static Action flipOut(final float x, final float width, final float duration) {
        return new Action() {
            float left = duration;

            @Override
            public boolean act(float delta) {
                left -= delta;
                if (left <= 0) {
                    actor.setX(x + (width / 2));
                    actor.setWidth(0);
                    return true;
                }
                float tmpWidth = width * (left / duration);
                actor.setX(x + ((width / 2) - (tmpWidth / 2)));
                actor.setWidth(tmpWidth);
                return false;
            }
        };
    }

    public static Action flipIn(final float x, final float width, final float duration) {
        return new Action() {
            float done = 0;

            @Override
            public boolean act(float delta) {
                done += delta;
                if (done >= duration) {
                    actor.setX(x);
                    actor.setWidth(width);
                    return true;
                }
                float tmpWidth = width * (done / duration);
                actor.setX(x + ((width / 2) - (tmpWidth / 2)));
                actor.setWidth(tmpWidth);
                return false;
            }
        };
    }
}

You can combine these actions with

myActor.addAction(
    new SequenceAction(
        MyActions.flipOut(x, width, duration),
        MyActions.flipIn(x, width, duration)));

If you want your card to have different sides, you would need to perform an average action to switch the actor’s image.

, , :

SequenceAction action = new SequenceAction(
    new ParallelAction(
        Actions.scaleTo(0, 1, duration), 
        Actions.moveBy(width / 2, 0, duration)),
    new ParallelAction(
        Actions.scaleTo(1, 1, duration), 
        Actions.moveBy(-width / 2, 0, duration)));
+4

, , . :

    public void flip() {
        if(Graphic1.getWidth() == 0) {
             Graphic2.setWidth(Graphic2.getWidth()+5);
        } else {
             Graphic1.setWidth(Graphic1.getWidth()-5);
        }
    }

flip() render(), if(), . - :

    @Override
    public void render() { 
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
        boolean ButtonPressed = false;

        if(ButtonPressed) { 
            flip(); 
        } 

    }

ButtonPressed eventHandler . true. , , , eventHandler render(), ButtonPressed , .

    public class Demo {

        static boolean ButtonPressed = false;

        public void onCreate() {
        ...

, , , flip(), , , .

, !

+1

Source: https://habr.com/ru/post/1527892/


All Articles