未找到目标的 TweenAccessor
No TweenAccessor was found for the target
我在 libGdx 程序中实施补间时遇到问题。我的问题与 this 相似但不同。我正在关注这方面的视频指南,代码看起来确实相同,但我收到错误 No TweenAccessor was found for the target
- 我曾尝试在线寻找解决方案,但 none 有效并且大部分错误地将补间设置为 class.
代码:
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.run(LwjglApplication.java:120)
如您所见(如链接问题中所示),补间设置为 class,如我的打印所示。我在这里做错了什么?
您需要将 SpriteAccessor 注册到 Sprite class 而不是 SplashScreen class。您正在执行的补间是在精灵上而不是在屏幕上。
改变
Tween.registerAccessor(SplashScreen.class, new SpriteAccessor());
至
Tween.registerAccessor(Sprite.class, new SpriteAccessor());
我在 libGdx 程序中实施补间时遇到问题。我的问题与 this 相似但不同。我正在关注这方面的视频指南,代码看起来确实相同,但我收到错误 No TweenAccessor was found for the target
- 我曾尝试在线寻找解决方案,但 none 有效并且大部分错误地将补间设置为 class.
代码:
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.run(LwjglApplication.java:120)
如您所见(如链接问题中所示),补间设置为 class,如我的打印所示。我在这里做错了什么?
您需要将 SpriteAccessor 注册到 Sprite class 而不是 SplashScreen class。您正在执行的补间是在精灵上而不是在屏幕上。
改变
Tween.registerAccessor(SplashScreen.class, new SpriteAccessor());
至
Tween.registerAccessor(Sprite.class, new SpriteAccessor());