Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mygdx.game;
- import com.badlogic.gdx.ApplicationListener;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.g2d.Animation;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- public class MyGdxGame implements ApplicationListener {
- public static final int COL_INT = 21;
- public static final int ROW_INT = 2;
- Animation mAnimation;
- Texture mTexture;
- TextureRegion[][] trWalking1;
- TextureRegion[] trWalking2;
- TextureRegion currentFrame;
- SpriteBatch spriteBatch;
- float stateTime = 0;
- float halfHeight;
- float aspectRatio;
- int newWidth;
- @Override
- public void resize(int width, int height) {
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void create() {
- mTexture = new Texture(Gdx.files.internal("super-mario-sprite.png"));
- trWalking1 = TextureRegion.split(mTexture, mTexture.getWidth()/COL_INT, mTexture.getHeight()/ROW_INT);
- trWalking2 = new TextureRegion[COL_INT * ROW_INT];
- int index = 0;
- for (int row = 0; row < ROW_INT; row++) {
- for (int col = 0; col < COL_INT; col++) {
- trWalking2[index++] = trWalking1[row][col];
- }
- }
- mAnimation = new Animation(1/9f, trWalking2);
- spriteBatch = new SpriteBatch();
- float textureRegionWidth = mTexture.getWidth()/COL_INT;
- float textureRegionHeight = mTexture.getHeight()/ROW_INT;
- aspectRatio = textureRegionHeight/textureRegionWidth;
- float screenHeight = (float) Gdx.graphics.getHeight();
- halfHeight = screenHeight/2f;
- newWidth = Math.round(halfHeight /aspectRatio);
- }
- @Override
- public void dispose() {
- mTexture.dispose();
- spriteBatch.dispose();
- }
- @Override
- public void render() {
- Gdx.gl.glClearColor(1, 1, 1, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- stateTime += Gdx.graphics.getDeltaTime();
- currentFrame = mAnimation.getKeyFrame(stateTime, true);
- spriteBatch.begin();
- spriteBatch.draw(currentFrame, 300, 300, newWidth, halfHeight);
- //, Gdx.graphics.getWidth()/8f, Gdx.graphics.getHeight()/4f
- spriteBatch.end();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement