S
S
Satangelus2018-08-06 03:34:22
Java
Satangelus, 2018-08-06 03:34:22

How to make in java follow method calls (ala sequence in tween engine)?

I want to make my own "bicycle" to control the animation. I wondered how to make a sequence of calls in Java simpler and more elegant.
Or, to be honest, how do you go about implementing such a construct?
Something like this:
AdvanceSprite.createSequence()
.set(x,y)
.move(x1,y1)
.pause(1000)
.set(x2,y2)
.hide()
.start()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivan19631224, 2018-08-06
@Satangelus

Very simple, you just need to return the object itself that does the operations:

public static Sequence createSequence() {
      return new Sequence();
    }
    
    public static class Sequence {
      public Sequence set(int x, int y) {
        // do something with x, y
        return this;
      }

      public Sequence move(int x, int y) {
        // do something with x, y
        return this;
      }
      //...
    }

This is called "method chaining".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question