Created
January 11, 2016 14:52
-
-
Save Ripley6811/138f0b64a219fd731d1c to your computer and use it in GitHub Desktop.
Exercise 2.1.08 alternative using "createSprite". Notice that using the Sprite's draw method does a better job than SpriteBatch's draw with scaling.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.udacity.gamedev.textureatlas; | |
import com.badlogic.gdx.ApplicationAdapter; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.assets.AssetDescriptor; | |
import com.badlogic.gdx.assets.AssetErrorListener; | |
import com.badlogic.gdx.assets.AssetManager; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.g2d.Animation; | |
import com.badlogic.gdx.graphics.g2d.Sprite; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.graphics.g2d.TextureAtlas; | |
import com.badlogic.gdx.utils.Array; | |
public class TextureAtlasExercise extends ApplicationAdapter implements AssetErrorListener { | |
public static final String TAG = TextureAtlasExercise.class.getName(); | |
private static final String ATLAS = "images/gigagal.pack.atlas"; | |
private static final String STANDING_RIGHT = "standing-right"; | |
// TODO: Add an AssetManager | |
private AssetManager assetManager; | |
SpriteBatch batch; | |
// TODO: Add a Sprite to hold the standing right sprite | |
Sprite standingRight; | |
Array<Sprite> walkRight; | |
int animTime = 0; | |
Animation animation; | |
@Override | |
public void create() { | |
batch = new SpriteBatch(); | |
// TODO: Initialize your AssetManager | |
assetManager = new AssetManager(); | |
// TODO: Set this as the AssetManager's error listener | |
assetManager.setErrorListener(this); | |
// TODO: tell the AssetManager to load the TextureAtlas with name ATLAS | |
assetManager.load(ATLAS, TextureAtlas.class); | |
// TODO: Call finishLoading() on your AssetManager | |
assetManager.finishLoading(); | |
// TODO: Get the TextureAtlas from the asset manager | |
TextureAtlas atlas = assetManager.get(ATLAS); | |
// TODO: Populate your ~ using your Atlas | |
standingRight = atlas.createSprite(STANDING_RIGHT); | |
walkRight = new Array<Sprite>(3); | |
for (int i=1; i<4; i++) { | |
walkRight.add(atlas.createSprite("walk-" + i + "-right")); | |
} | |
for (Sprite sprite: walkRight) { | |
sprite.setScale(3f); | |
sprite.setX(100f); | |
sprite.setOrigin(sprite.getOriginX(), 0f); | |
} | |
animation = new Animation(1, walkRight, Animation.PlayMode.LOOP_PINGPONG); | |
} | |
@Override | |
public void render() { | |
Gdx.gl.glClearColor(1, 1, 1, 1); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
batch.begin(); | |
// TODO: Draw the standing right AtlasRegion | |
standingRight.draw(batch); | |
// Drawing with the Sprite.draw method. Uses the preset scaling, origin, etc. | |
walkRight.get(animTime / 10).draw(batch); | |
// Drawing the Sprite's Texture. This ignores Sprite scaling, origin, etc. | |
float scale = 3f; | |
batch.draw(animation.getKeyFrame(animTime / 10), 200, 0, | |
scale*standingRight.getWidth(), scale*standingRight.getHeight()); | |
batch.end(); | |
animTime++; | |
if (animTime >= 30) animTime = 0; | |
} | |
@Override | |
public void error(AssetDescriptor asset, Throwable throwable) { | |
Gdx.app.error(TAG, "Couldn't load asset: " + asset.fileName, throwable); | |
} | |
@Override | |
public void dispose() { | |
batch.dispose(); | |
// TODO: Dispose of the AssetManager | |
assetManager.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment