Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. package com.mygdx.game;
  2.  
  3. import com.badlogic.gdx.ApplicationListener;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.g2d.Animation;
  7. import com.badlogic.gdx.graphics.Texture;
  8. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  9. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  10.  
  11. public class MyGdxGame implements ApplicationListener {
  12.  
  13. public static final int COL_INT = 21;
  14. public static final int ROW_INT = 2;
  15.  
  16. Animation mAnimation;
  17. Texture mTexture;
  18. TextureRegion[][] trWalking1;
  19. TextureRegion[] trWalking2;
  20. TextureRegion currentFrame;
  21. SpriteBatch spriteBatch;
  22. float stateTime = 0;
  23. float halfHeight;
  24. float aspectRatio;
  25. int newWidth;
  26.  
  27. @Override
  28. public void resize(int width, int height) {
  29.  
  30. }
  31.  
  32. @Override
  33. public void pause() {
  34.  
  35. }
  36.  
  37. @Override
  38. public void resume() {
  39.  
  40. }
  41.  
  42.  
  43.  
  44. @Override
  45. public void create() {
  46. mTexture = new Texture(Gdx.files.internal("super-mario-sprite.png"));
  47. trWalking1 = TextureRegion.split(mTexture, mTexture.getWidth()/COL_INT, mTexture.getHeight()/ROW_INT);
  48. trWalking2 = new TextureRegion[COL_INT * ROW_INT];
  49.  
  50. int index = 0;
  51.  
  52. for (int row = 0; row < ROW_INT; row++) {
  53. for (int col = 0; col < COL_INT; col++) {
  54. trWalking2[index++] = trWalking1[row][col];
  55. }
  56. }
  57.  
  58. mAnimation = new Animation(1/9f, trWalking2);
  59.  
  60. spriteBatch = new SpriteBatch();
  61.  
  62. float textureRegionWidth = mTexture.getWidth()/COL_INT;
  63.  
  64. float textureRegionHeight = mTexture.getHeight()/ROW_INT;
  65.  
  66. aspectRatio = textureRegionHeight/textureRegionWidth;
  67.  
  68. float screenHeight = (float) Gdx.graphics.getHeight();
  69.  
  70. halfHeight = screenHeight/2f;
  71.  
  72. newWidth = Math.round(halfHeight /aspectRatio);
  73. }
  74.  
  75. @Override
  76. public void dispose() {
  77. mTexture.dispose();
  78. spriteBatch.dispose();
  79. }
  80.  
  81. @Override
  82. public void render() {
  83.  
  84. Gdx.gl.glClearColor(1, 1, 1, 1);
  85. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  86.  
  87. stateTime += Gdx.graphics.getDeltaTime();
  88.  
  89. currentFrame = mAnimation.getKeyFrame(stateTime, true);
  90.  
  91. spriteBatch.begin();
  92. spriteBatch.draw(currentFrame, 300, 300, newWidth, halfHeight);
  93. //, Gdx.graphics.getWidth()/8f, Gdx.graphics.getHeight()/4f
  94. spriteBatch.end();
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement