Skip to content

Instantly share code, notes, and snippets.

@RichardMarks
Created February 23, 2015 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardMarks/c212d6270df2f2b226de to your computer and use it in GitHub Desktop.
Save RichardMarks/c212d6270df2f2b226de to your computer and use it in GitHub Desktop.
using universal tween engine and libgdx
package com.mygdx.game;
import aurelienribon.tweenengine.*;
import aurelienribon.tweenengine.equations.Bounce;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
Sprite bad;
TweenManager tweenManager;
@Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
bad = new Sprite(img);
bad.setPosition(32, 0);
bad.setColor(1, 1, 0, 1);
bad.setAlpha(0.0f);
Tween.registerAccessor(Sprite.class, new SpriteAccessor());
tweenManager = new TweenManager();
//Tween.to(bad, SpriteAccessor.ALPHA, 3.0f).target(1.0f).start(tweenManager);
Timeline.createSequence()
.push(Tween.to(bad, SpriteAccessor.ALPHA, 3.0f).target(1.0f))
.pushPause(1.0f)
.push(Tween.to(bad, SpriteAccessor.P_XY, 1.0f).target(128, 128).ease(Bounce.OUT).setCallback(new TweenCallback() {
@Override
public void onEvent(int type, BaseTween<?> source) {
bad.setColor(1, 0, 0, 1);
}
}).setCallbackTriggers(TweenCallback.COMPLETE))
.pushPause(1.0f)
.push(Tween.set(bad, SpriteAccessor.ALPHA).target(0.0f))
.start(tweenManager);
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
tweenManager.update(Gdx.graphics.getDeltaTime());
batch.begin();
//batch.draw(img, 0, 0);
bad.draw(batch);
batch.end();
}
class SpriteAccessor implements TweenAccessor<Sprite> {
// tween the position
public static final int P_X = 1;
public static final int P_Y = 2;
public static final int P_XY = 3;
// tween the scale
public static final int S_X = 4;
public static final int S_Y = 5;
public static final int S_XY = 6;
// tween the alpha
public static final int ALPHA = 7;
@Override
public int getValues(Sprite target, int tweenType, float[] returnValues) {
switch (tweenType) {
case P_X: {
returnValues[0] = target.getX();
return 1;
}
case P_Y: {
returnValues[0] = target.getY();
return 1;
}
case P_XY: {
returnValues[0] = target.getX();
returnValues[1] = target.getY();
return 2;
}
case S_X: {
returnValues[0] = target.getScaleX();
return 1;
}
case S_Y: {
returnValues[0] = target.getScaleY();
return 1;
}
case S_XY: {
returnValues[0] = target.getScaleX();
returnValues[1] = target.getScaleY();
return 2;
}
case ALPHA: {
returnValues[0] = target.getColor().a;
return 1;
}
default:
assert false;
return -1;
}
}
@Override
public void setValues(Sprite target, int tweenType, float[] newValues) {
switch (tweenType) {
case P_X: {
target.setX(newValues[0]);
} break;
case P_Y: {
target.setY(newValues[0]);
} break;
case P_XY: {
target.setX(newValues[0]);
target.setY(newValues[1]);
} break;
case S_X: {
target.setScale(newValues[0], target.getScaleY());
} break;
case S_Y: {
target.setScale(target.getScaleX(), newValues[0]);
} break;
case S_XY: {
target.setScale(newValues[0], newValues[1]);
} break;
case ALPHA: {
target.setAlpha(newValues[0]);
} break;
default:
assert false;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment