LibGdx - Transition between Screens

前端 未结 4 930
孤城傲影
孤城傲影 2021-02-04 03:50

I\'m trying to change the animation between Libgdx Screens. I want to write my custom animation (fade in, fade out, etc). Can someone give me a clue? I can\'t seem to find the i

4条回答
  •  渐次进展
    2021-02-04 04:25

    This is a sample code of my game in transitions animation between screens: in MainGame class:

    @Override public void setScreen(final Screen screen) {
            if (getScreen() == null)
            {
                createScreenInAction(screen);
                return;
            }
            createScreenOutAction(getScreen(), new Runnable() {
                @Override public void run() {
                    createScreenInAction(screen);
                }
            });
    
        }
        private void createScreenOutAction(final Screen screen , Runnable runnable) {
            Actor actor = ((BaseScreenAdapter) screen).getStage().getRoot();
            actor.setOrigin(WIDTH_HALF,HEIGHT_HALF);
            actor.getColor().a = 1;
            SequenceAction sequenceAction = new SequenceAction();
            sequenceAction.addAction(Actions.parallel(Actions.alpha(0,SCREEN_SWITCH_DURATION) , Actions.scaleTo(1.5f,1.5f , SCREEN_SWITCH_DURATION, Interpolation.exp5)));
            sequenceAction.addAction(Actions.run(runnable));
            actor.addAction(sequenceAction);
        }
        private void createScreenInAction(final Screen screen) {
            StarsGame.super.setScreen(screen);
            Actor actor = ((BaseScreenAdapter) screen).getStage().getRoot();
            actor.setOrigin(WIDTH_HALF,HEIGHT_HALF);
            actor.getColor().a = 0;
            SequenceAction sequenceAction = new SequenceAction();
            sequenceAction.addAction(Actions.scaleTo(1.5f,1.5f , 0));
            sequenceAction.addAction(Actions.parallel(Actions.alpha(1,SCREEN_SWITCH_DURATION) , Actions.scaleTo(1.0f,1.0f , SCREEN_SWITCH_DURATION , Interpolation.exp5)));
            actor.addAction(sequenceAction);
    
        }
    

    and all my screens extends as BaseScreenAdapter:

    public abstract class BaseScreenAdapter extends ScreenAdapter implements BaseRequest.BaseResponseError{
        protected final AssetsController mAssets;
        protected final MySettings mSettings;
        protected StarsGame mGame;
        protected Stage mStage;
        protected Viewport mViewport;
        protected OrthographicCamera mCamera;
        protected InputMultiplexer multiplexer;
        protected LoadingActor mLoadingActor;
        //==============================================================
        // METHODS
        //==============================================================
        public BaseScreenAdapter(StarsGame game) {
            this.mGame = game;
            mCamera = new OrthographicCamera(/*StarsGame.WIDTH, StarsGame.HEIGHT*/);
            mCamera.position.set(StarsGame.WIDTH_HALF, StarsGame.HEIGHT, 0);
            mViewport = new FitViewport(StarsGame.WIDTH, StarsGame.HEIGHT, mCamera);
            mCamera.position.set(mCamera.viewportWidth / 2f, mCamera.viewportHeight / 2 , 0);
    
            initStage();
            initInputMultiplexer();
        }
    
        public Stage getStage() {
            return mStage;
        }
        private void initStage() {
            mStage = new Stage(mViewport);
            mStage.addListener(new InputListener() {
                @Override public boolean keyUp(InputEvent event, int keycode) {
                    if (keycode == Input.Keys.BACK)
                    {
                        onBackPressed();
                    }
                    return super.keyUp(event, keycode);
                }
            });
        }
    }
    

提交回复
热议问题