Advertisement
dermetfan

SpriteBatch fade transition

Oct 19th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package net.dermetfan.someLibgdxTests.screens;
  2.  
  3. import net.dermetfan.someLibgdxTests.tween.ColorAccessor;
  4. import aurelienribon.tweenengine.BaseTween;
  5. import aurelienribon.tweenengine.Tween;
  6. import aurelienribon.tweenengine.TweenCallback;
  7. import aurelienribon.tweenengine.TweenManager;
  8.  
  9. import com.badlogic.gdx.Game;
  10. import com.badlogic.gdx.Gdx;
  11. import com.badlogic.gdx.Screen;
  12. import com.badlogic.gdx.graphics.Color;
  13. import com.badlogic.gdx.graphics.GL20;
  14. import com.badlogic.gdx.graphics.OrthographicCamera;
  15. import com.badlogic.gdx.graphics.Pixmap;
  16. import com.badlogic.gdx.graphics.Pixmap.Format;
  17. import com.badlogic.gdx.graphics.Texture;
  18. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  19.  
  20. public class TransitionTest implements Screen {
  21.  
  22.     private SpriteBatch batch;
  23.     private OrthographicCamera cam;
  24.     private TweenManager tweenManager;
  25.  
  26.     private boolean transition;
  27.     private Texture transitionTexture;
  28.     private Color transitionColor;
  29.  
  30.     @Override
  31.     public void render(float delta) {
  32.         Gdx.gl.glClearColor(1, 0, 0, 1);
  33.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  34.         batch.begin();
  35.  
  36.         batch.setProjectionMatrix(cam.combined);
  37.  
  38.         // draw your stuff here
  39.  
  40.         // draw the black over all of it
  41.         if(transition) {
  42.             tweenManager.update(delta);
  43.             batch.setColor(transitionColor);
  44.             batch.draw(transitionTexture, cam.position.x - cam.viewportWidth / 2, cam.position.y - cam.viewportHeight / 2, cam.viewportWidth, cam.viewportHeight);
  45.         }
  46.  
  47.         batch.end();
  48.     }
  49.  
  50.     private void startTransition() {
  51.         transition = true;
  52.         Tween.to(transitionColor, ColorAccessor.A, 1).target(1).setCallback(new TweenCallback() {
  53.  
  54.             @Override
  55.             public void onEvent(int type, BaseTween<?> source) {
  56.                 ((Game) Gdx.app.getApplicationListener()).setScreen(new Menu());
  57.             }
  58.  
  59.         }).start(tweenManager);
  60.     }
  61.  
  62.     @Override
  63.     public void resize(int width, int height) {
  64.         startTransition();
  65.     }
  66.  
  67.     @Override
  68.     public void show() {
  69.         batch = new SpriteBatch();
  70.         cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  71.  
  72.         // you could also just load a black texture the normal way
  73.         Pixmap pixmap = new Pixmap(1, 1, Format.RGB565);
  74.         pixmap.setColor(Color.BLACK);
  75.         pixmap.fill();
  76.         transitionTexture = new Texture(pixmap);
  77.         pixmap.dispose();
  78.  
  79.         transitionColor = new Color(0, 0, 0, 0);
  80.  
  81.         tweenManager = new TweenManager();
  82.         Tween.registerAccessor(Color.class, new ColorAccessor());
  83.     }
  84.  
  85.     @Override
  86.     public void hide() {
  87.         dispose();
  88.     }
  89.  
  90.     @Override
  91.     public void pause() {
  92.     }
  93.  
  94.     @Override
  95.     public void resume() {
  96.     }
  97.  
  98.     @Override
  99.     public void dispose() {
  100.         batch.dispose();
  101.         transitionTexture.dispose();
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement