LibGdx - 屏幕之间的转换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LibGdx - 屏幕之间的转换相关的知识,希望对你有一定的参考价值。
我正在尝试更改Libgdx屏幕之间的动画。我想写自定义动画(淡入淡出,淡出等)。有人能给我一个线索吗?我似乎无法在Libgdx代码中找到过渡的实现。
我使用Scene2D和通用补间引擎实现了一些滑动过渡。你可以在这里找到示例代码。
http://www.netthreads.co.uk/2012/02/09/libgdx-scene2d-demo-with-scene-transitions/
更新:本文说明了我实现滑动过渡的方法。底部有一个链接,带您到https://github.com/alistairrutherford/libgdx-demos的一组演示
有关如何构建演示的明确说明,但您需要至少掌握maven以及如何设置它。
这是我做的:
FadeIn非常简单,只需将其添加到你的fadein Screens show():
stage.getRoot().getColor().a = 0;
stage.getRoot().addAction(fadeIn(0.5f));
FadeOut有点棘手。您不想立即切换屏幕,因此不要调用game.setScreen(newScreen)在您的淡出屏幕中创建一个方法,如下所示:
public void switchScreen(final Game game, final Screen newScreen){
stage.getRoot().getColor().a = 1;
SequenceAction sequenceAction = new SequenceAction();
sequenceAction.addAction(fadeOut(0.5f));
sequenceAction.addAction(run(new Runnable() {
@Override
public void run() {
game.setScreen(newScreen);
}
}));
stage.getRoot().addAction(sequenceAction);
}
像这样,您可以在淡出期间延迟屏幕切换。
我已经实现了类似的方法。感谢Gustavo Steigert,我从他的博客中学到了很多东西,在那里你可以找到他的fadeIn和fadeOut序列的例子。
http://steigert.blogspot.in/2012/02/3-libgdx-tutorial-scene2d.html
您可以完全按照他的博客来更好地了解事物的流程,并在每个帖子的帖子中找到帖子源代码的标签。
这是我的游戏在屏幕之间的过渡动画中的示例代码:在MainGame
类中:
@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);
}
我的所有屏幕都延伸为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);
}
});
}
}
以上是关于LibGdx - 屏幕之间的转换的主要内容,如果未能解决你的问题,请参考以下文章