Search code examples
libgdxrunnable

can't run 2 runnable action in same ActionSequence


I was trying to run 2 Runnable action in the same action sequence, but I get an error, saying that it cannot resolve constructor SuquenceAction.. Not sure if I missed something entirely obvious and merely having bad sight, but it appears that one can not run 2 Runnable action in sequence. What is the workaround? My sincere apologies if I am missing a syntax error here.

error:

no suitable constructor found for SequenceAction(ColorAction,RunnableAction,ColorAction,ColorAction,ColorAction,RunnableAction,ColorAction)

ColorAction colorToBlack= new ColorAction();
                    colorToBlack.setDuration(0.2f);
                    colorToBlack.setEndColor(Color.BLACK);
                    colorToBlack.setTime(0.2f);

                    ColorAction colorToWhite = new ColorAction();
                    colorToWhite.setDuration(0.2f);
                    colorToWhite.setEndColor(Color.WHITE);
                    colorToWhite.setTime(0.2f);

                    ColorAction wait = new ColorAction();
                    wait.setDuration(2.5f);
                    wait.setTime(2.5f);

                    ColorAction colorToBlack2= new ColorAction();
                    colorToBlack2.setDuration(0.2f);
                    colorToBlack2.setEndColor(Color.BLACK);
                    colorToBlack2.setTime(0.2f);

                    ColorAction colorToWhite2 = new ColorAction();
                    colorToWhite2.setDuration(0.2f);
                    colorToWhite2.setEndColor(Color.WHITE);
                    colorToWhite2.setTime(0.2f);

                SequenceAction sq = new SequenceAction(colorToBlack,Actions.run(new Runnable() {
                    @Override
                    public void run() {

                    }
                }),colorToWhite,wait,colorToBlack2,Actions.run(new Runnable() {
                    @Override
                    public void run() {

                    }
                }),colorToWhite2);

Solution

  • As you can see in the error message, No constructor found. It is because there is no constructor in SequenceAction class which can take 7 Actions. The maximum is 5.

    Here is the JavaDoc for SequenceAction

    The solution for this is, create SequenceAction object and add Actions 1 by 1 in order.

    SequenceAction sq = new SequenceAction();
    
    sq.addAction(colorToBlack);
    sq.addAction(Actions.run(() -> {/**Using lambda expression, it's similar to New Runnable() { run() } **/} ));
    sq.addAction(colorToWhite);
    sq.addAction(wait);
    sq.addAction(colorToBlack2);
    sq.addAction(Actions.run(() -> {}));
    sq.addAction(colorToWhite2);
    

    As if you look into LibGdx code, in the constructor what they are doing is

    public SequenceAction (Action action1, Action action2, Action action3, Action action4, Action action5) {
            addAction(action1);
            addAction(action2);
            addAction(action3);
            addAction(action4);
            addAction(action5);
    }
    

    It is similar to what I gave you.

    I hope it helped :)