我在libGdx程序中实现补间问题。我的问题与this类似但不同。我正在关注此视频指南并且代码看起来确实相同但是我收到错误No TweenAccessor was found for the target
- 我尝试在线寻找解决方案,但没有一个工作,并且大多错误地将补间设置为类。
代码:
public class SplashScreen implements Screen {
...
private Sprite splash;
private TweenManager tweenManager;
...
@Override
public void show() {
batch = new SpriteBatch();
tweenManager = new TweenManager();
Tween.registerAccessor(SplashScreen.class, new SpriteAccessor());
System.out.println(Tween.getRegisteredAccessor(SplashScreen.class));
Texture splashTexture = new Texture(Gdx.files.internal("img/splash.png"));
splash = new Sprite(splashTexture);
splash.setSize(Constants.APP_WIDTH, Constants.APP_HEIGHT);
Tween.set(splash, SpriteAccessor.ALPHA).target(0).start(tweenManager); // This throws the error
Tween.to(splash, SpriteAccessor.ALPHA, 2).target(1).start(tweenManager);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
tweenManager.update(delta);
batch.begin();
splash.draw(batch);
batch.end();
}
...
访问器:
public class SpriteAccessor implements TweenAccessor<Sprite> {
public static final int ALPHA = 0;
@Override
public int getValues(Sprite target, int tweenType, float[] returnValues) {
switch(tweenType) {
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 ALPHA:
target.setColor(target.getColor().r, target.getColor().g, target.getColor().b, newValues[0]);
break;
default:
assert false;
}
}
logcat的:
tween.SpriteAccessor@1b3a40d // note
Exception in thread "LWJGL Application" java.lang.RuntimeException: No TweenAccessor was found for the target
at aurelienribon.tweenengine.Tween.build(Tween.java:787)
at aurelienribon.tweenengine.Tween.build(Tween.java:79)
at aurelienribon.tweenengine.BaseTween.start(BaseTween.java:85)
at aurelienribon.tweenengine.TweenManager.add(TweenManager.java:61)
at aurelienribon.tweenengine.BaseTween.start(BaseTween.java:98)
at screens.SplashScreen.show(SplashScreen.java:36)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.simcas.game.RunGame.create(RunGame.java:12)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
正如您所看到的(如在链接的问题中),补间设置为我的打印显示的类。我在这里做错了什么?
答案 0 :(得分:3)
您需要将SpriteAccessor注册到Sprite类而不是SplashScreen类。您正在执行的补间是在不在屏幕上的精灵上。
更改
Tween.registerAccessor(SplashScreen.class, new SpriteAccessor());
到
Tween.registerAccessor(Sprite.class, new SpriteAccessor());