javalibgdxstatic-import

how to ignore Actor methods and use static import of Actions?


when I use static import of import com.badlogic.gdx.scenes.scene2d.actions.Actions;

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
import com.badlogic.gdx.scenes.scene2d.Actor;


public class EnemyCar extends Actor {
    private Rectangle bounds = new Rectangle();
    
    public EnemyCar(float x, float y) {
        setWidth(160);
        setHeight(85);
        setPosition(x, y - getHeight()/2);

        int rnd = MathUtils.random(0, 3);
        if (rnd == 0) setColor(Color.RED);
        if (rnd == 1) setColor(Color.GREEN);
        if (rnd == 2) setColor(Color.WHITE);
        if (rnd == 3) setColor(Color.BLUE);
        
        addAction(moveTo(-getWidth(), getY(), MathUtils.random(4.0f, 6.0f)));
    }
    
    public void crash(boolean front, boolean above) {
        clearActions();
        addAction(fadeOut(1f));

        // Here using Actions static import code
        if (front && above) addAction(sequence(Actions.parallel(Actions.rotateBy(-360, 1.5f), Actions.moveBy(200, 200, 1.5f)), Actions.removeActor()));
        Actions.rotate
        if (front && above) addAction(sequence(parallel(Actions.rotateBy(-360, 1.5f), moveBy(200, 200, 1.5f)), removeActor()));
        if (front && !above) addAction(sequence(parallel(rotateBy(360, 1.5f), moveBy(200, -200, 1.5f)), removeActor()));
        if (!front && above) addAction(sequence(parallel(rotateBy(360, 1.5f), moveBy(-200, 200, 1.5f)), removeActor()));
        if (!front && !above) addAction(sequence(parallel(rotateBy(-360, 1.5f), moveBy(-200, -200, 1.5f)), removeActor()));
    }
}

Actor.rotateBy() is called but I want to call Actions.rotateBy() Actions.rotateBy() works fine but I need to type Actions. in all methods.

How to ignore Actor.rotateBy() and call Actions::rotateBy(), moveBy(), sequence(), etc.


Solution

  • This is a limitation of Java. You can't.

    One possible work-around is to create another class with static pass-through methods for the static methods in Actions, with slightly different names.

    public final class InActorActions {
        static public RotateByAction $rotateBy (float rotationAmount) {
            return Actions.rotateBy(rotationAmount);
        }
    }
    

    Then do a static import of this class and use the different method name.