Advertisement
dermetfan

How to create an animated button (boobool's Android crash)

Jun 27th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package net.dermetfan.someLibgdxTests.screens;
  2.  
  3. import net.dermetfan.libgdx.graphics.AnimatedSprite;
  4. import net.dermetfan.someLibgdxTests.SomeLibgdxTests;
  5.  
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.Screen;
  8. import com.badlogic.gdx.graphics.GL20;
  9. import com.badlogic.gdx.graphics.Texture;
  10. import com.badlogic.gdx.graphics.g2d.Animation;
  11. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  12. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  13. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  14. import com.badlogic.gdx.scenes.scene2d.Stage;
  15. import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
  16. import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
  17. import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
  18. import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
  19.  
  20. public class AnimatedButtonTest implements Screen {
  21.    
  22.     private Stage stage;
  23.  
  24.     @Override
  25.     public void render(float delta) {
  26.         Gdx.gl.glClearColor(0, 0, 0, 1);
  27.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  28.        
  29.         stage.act(delta);
  30.         stage.draw();
  31.     }
  32.  
  33.     @Override
  34.     public void resize(int width, int height) {
  35.     }
  36.  
  37.     @Override
  38.     public void show() {
  39.         stage = new Stage();
  40.        
  41.         Gdx.input.setInputProcessor(stage);
  42.        
  43.         // create the animation for the AnimatedSprite
  44.         Animation animation = new Animation(1 / 3f, new TextureRegion(new Texture("img/1.png")), new TextureRegion(new Texture("img/2.png")), new TextureRegion(new Texture("img/3.png"));
  45.         animation.setPlayMode(Animation.LOOP);
  46.        
  47.         // create the AnimatedSprite and set its keepSize to true
  48.         AnimatedSprite animatedSprite = new AnimatedSprite(animation);
  49.         animatedSprite.setKeepSize(true);
  50.        
  51.         // create the TextButtonStyle for the TextButton and set the up Drawable to a SpriteDrawable to pass the AnimatedSprite in
  52.         TextButtonStyle tbs = new TextButtonStyle();
  53.         tbs.font = new BitmapFont();
  54.         tbs.up = new SpriteDrawable(animatedSprite);
  55.  
  56.         TextButton button = new TextButton("TEST", tbs);
  57.         // add some test listener to the button
  58.         button.addListener(new ClickListener() {
  59.             @Override
  60.             public void clicked(InputEvent event, float x, float y) {
  61.                 System.out.println("clicked");
  62.             }
  63.         });
  64.        
  65.         stage.addActor(button);
  66.     }
  67.  
  68.     @Override
  69.     public void hide() {
  70.         dispose();
  71.     }
  72.  
  73.     @Override
  74.     public void pause() {
  75.     }
  76.  
  77.     @Override
  78.     public void resume() {
  79.     }
  80.  
  81.     @Override
  82.     public void dispose() {
  83.         stage.dispose();
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement