daily pastebin goal
38%
SHARE
TWEET

LibGDX sample

a guest Apr 25th, 2016 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ua.com.vendetta8247.libgdxtest;
  2.  
  3. import com.badlogic.gdx.ApplicationAdapter;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input;
  6. import com.badlogic.gdx.InputProcessor;
  7. import com.badlogic.gdx.graphics.Color;
  8. import com.badlogic.gdx.graphics.GL20;
  9. import com.badlogic.gdx.graphics.OrthographicCamera;
  10. import com.badlogic.gdx.graphics.Texture;
  11. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  12. import com.badlogic.gdx.graphics.g2d.Sprite;
  13. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  14. import com.badlogic.gdx.graphics.g2d.TextureAtlas;
  15.  
  16. import com.badlogic.gdx.math.Vector2;
  17. import com.badlogic.gdx.scenes.scene2d.Stage;
  18. import com.badlogic.gdx.scenes.scene2d.ui.Skin;
  19. import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
  20. import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
  21. import com.badlogic.gdx.utils.Timer;
  22. import com.badlogic.gdx.utils.viewport.ExtendViewport;
  23. import com.badlogic.gdx.utils.viewport.FillViewport;
  24. import com.badlogic.gdx.utils.viewport.ScreenViewport;
  25. import com.badlogic.gdx.utils.viewport.StretchViewport;
  26. import com.badlogic.gdx.utils.viewport.Viewport;
  27.  
  28. public class MyGdxGame extends ApplicationAdapter {
  29.     SpriteBatch batch;
  30.     Sprite characterSprite;
  31.     float clearRed = 1, clearGreen = 1, clearBlue = 1;
  32.     boolean movingRight = true;
  33.     float velocityX = 3, velocityY = 20;
  34.     private int currentFrame = 0;
  35.     private TextureAtlas textureAtlas;
  36.     private String currentAtlasKey = new String("0001");
  37.     long startTime = 0;
  38.     long animTime = 0;
  39.     float multiplier = 0.3f;
  40.  
  41.  
  42.     Texture background;
  43.     Sprite bgSprite[];
  44.     Sprite bgSprite2;
  45.  
  46.  
  47.     private Stage stage;
  48.     private Touchpad touchpad;
  49.     private Touchpad.TouchpadStyle touchpadStyle;
  50.     private Skin touchpadSkin;
  51.     private Drawable touchBackground;
  52.     private Drawable touchKnob;
  53.     private BitmapFont font;
  54.     private Viewport viewport;
  55.     OrthographicCamera camera;
  56.  
  57.     @Override
  58.     public void create() {
  59.         Gdx.graphics.setVSync(true);
  60.         camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  61.         viewport = new ExtendViewport(1920, 1080, camera);
  62.         viewport.apply();
  63.         viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  64.         batch = new SpriteBatch();
  65.  
  66.         bgSprite2 = new Sprite(new Texture("bg2.png"));
  67.         bgSprite2.setPosition(0, 0);
  68.         bgSprite2.setSize(bgSprite2.getWidth() * 2, bgSprite2.getHeight() * 2);
  69.  
  70.         background = new Texture("bg.png");
  71.         bgSprite = new Sprite[2];
  72.         bgSprite[0] = new Sprite(background);
  73.         bgSprite[1] = new Sprite(background);
  74.  
  75.         bgSprite[0].setSize(bgSprite[0].getWidth() * 2, bgSprite[0].getHeight() * 2);
  76.         bgSprite[1].setSize(bgSprite[1].getWidth() * 2, bgSprite[1].getHeight() * 2);
  77.         bgSprite[0].setPosition(0, -200);
  78.         bgSprite[1].setPosition(bgSprite[0].getX() + bgSprite[0].getWidth(), -200);
  79.  
  80.         camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
  81.         camera.position.set(0, 0, 0);
  82.         camera.update();
  83.  
  84.  
  85.         font = new BitmapFont();
  86.  
  87.         font.setColor(Color.RED);
  88.  
  89.  
  90.         touchpadSkin = new Skin();
  91.         touchpadSkin.add("touchBackground", new Texture("touchBackground.png"));
  92.         touchpadSkin.add("touchKnob", new Texture("touchKnob.png"));
  93.         touchpadStyle = new Touchpad.TouchpadStyle();
  94.         touchBackground = touchpadSkin.getDrawable("touchBackground");
  95.         touchKnob = touchpadSkin.getDrawable("touchKnob");
  96.         touchpadStyle.background = touchBackground;
  97.         touchpadStyle.knob = touchKnob;
  98.         touchpad = new Touchpad(10, touchpadStyle);
  99.         touchpad.setBounds(50, 50, 300, 300);
  100.  
  101.  
  102.         stage = new Stage(viewport, batch);
  103.         stage.addActor(touchpad);
  104.         Gdx.input.setInputProcessor(stage);
  105.  
  106.         startTime = System.currentTimeMillis();
  107.  
  108.         textureAtlas = new TextureAtlas(Gdx.files.internal("ready.atlas"));
  109.         TextureAtlas.AtlasRegion region = textureAtlas.findRegion("1");
  110.  
  111.  
  112.         characterSprite = new Sprite(region);
  113.  
  114.         characterSprite.setPosition(100, 100);
  115.         characterSprite.setSize(characterSprite.getWidth() * 3, characterSprite.getHeight() * 3);
  116.  
  117.  
  118.         Timer.schedule(new Timer.Task() {
  119.             @Override
  120.             public void run() {
  121.  
  122.                 if (System.currentTimeMillis() - startTime > 80) {
  123.                     currentFrame++;
  124.                     startTime = System.currentTimeMillis();
  125.                 }
  126.                 if (currentFrame > 7)
  127.                     currentFrame = 0;
  128.  
  129.                 currentAtlasKey = String.format("%d", currentFrame);
  130.                 characterSprite.setRegion(textureAtlas.findRegion(currentAtlasKey));
  131.  
  132.                 //characterSprite.setPosition(characterSprite.getX() + velocity,characterSprite.getY());
  133.  
  134.                 //TODO collision
  135. //              if(characterSprite.getX() + characterSprite.getWidth()>=Gdx.graphics.getWidth())
  136. //                  characterSprite.setPosition(Gdx.graphics.getWidth() - characterSprite.getWidth() + 2, characterSprite.getY());
  137.             }
  138.         }, 0f, 1 / 60f);
  139.     }
  140.  
  141.  
  142.     @Override
  143.     public void render() {
  144.  
  145.  
  146.         Gdx.gl.glClearColor(85.0f / 255.0f, 200 / 255.0f, 240.0f / 255, 1);
  147.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  148.  
  149.  
  150.         update();
  151.         draw();
  152.     }
  153.  
  154.     public void draw() {
  155.         batch.setProjectionMatrix(camera.combined);
  156.  
  157.         batch.begin();
  158.         bgSprite2.draw(batch);
  159.         //touchpad.draw(batch,1.0f);
  160.         bgSprite[0].draw(batch);
  161.         bgSprite[1].draw(batch);
  162.         characterSprite.draw(batch);
  163.  
  164.  
  165.         batch.end();
  166.     }
  167.  
  168.     public void update() {
  169.         System.out.println(bgSprite[0].getX());
  170.         camera.update();
  171.         if (characterSprite.getX() > bgSprite[1].getX() + bgSprite[1].getWidth() + 100) {
  172.             bgSprite[1].setPosition(bgSprite[0].getX() + bgSprite[0].getWidth(), -200);
  173.         }
  174.  
  175.         if (characterSprite.getX() > bgSprite[0].getX() + bgSprite[0].getWidth() + 100) {
  176.             bgSprite[0].setPosition(bgSprite[1].getX() + bgSprite[0].getWidth(), -200);
  177.         }
  178.  
  179.         if (Gdx.input.isTouched()) {
  180.  
  181.             if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2) {
  182.                 velocityX = 15;
  183.             } else
  184.                 multiplier = 0.6f;
  185.         } else {
  186.             velocityX = 5;
  187.             multiplier = 0.3f;
  188.         }
  189.         if (System.currentTimeMillis() - animTime >= 1000 / 60) {
  190.             bgSprite2.translate(velocityX, 0);
  191.             //camera.translate(velocityX, 0);
  192.             camera.position.x += velocityX;
  193.  
  194.             velocityY -= multiplier;
  195.             animTime = System.currentTimeMillis();
  196.             if (characterSprite.getY() < 5)
  197.                 velocityY = 20;
  198.  
  199.             characterSprite.setPosition(characterSprite.getX() + velocityX, characterSprite.getY() + velocityY);
  200.         }
  201.  
  202.  
  203.         //characterSprite.setPosition(characterSprite.getX() + velocityX, characterSprite.getY()+velocityY);
  204. //
  205. //      if (!movingRight)
  206. //          characterSprite.flip(true, false);
  207. //
  208. //      if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
  209. //          if(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
  210. //              characterSprite.translateX(-1f);
  211. //          else
  212. //              characterSprite.translateX(-2.0f);
  213. //          movingRight = false;
  214. //      }
  215. //      if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
  216. //          if(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
  217. //              characterSprite.translateX(1f);
  218. //          else
  219. //              characterSprite.translateX(2.0f);
  220. //          movingRight = true;
  221. //      }
  222. //
  223. //
  224. //
  225. //      characterSprite.setX(characterSprite.getX() + touchpad.getKnobPercentX()*5);
  226. //      if(touchpad.getKnobPercentX()<0)
  227. //          movingRight = false;
  228. //      else if(touchpad.getKnobPercentX()>0) movingRight = true;
  229. //      //characterSprite.setY(characterSprite.getY() + touchpad.getKnobPercentY()*5);
  230.     }
  231. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top