我正在尝试在Android上运行一个简单的libgdx项目。一切都很好,但我的InputProcessor不会触发它的事件。 我根据本教程实现了一切: http://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements3TierProjectSetup#Code_Example
第一次调用“showToast”工作正常并显示在我的屏幕上=> showToast-Method确实有效。不幸的是,我无法触发任何InputProcessor事件。即使调试器也不止于此,因此绝对不会调用它们。
编辑:这是完整的代码。我只省略了计算器类,因为它工作正常,不应该在这里任何conern。如果有人不同意,我当然可以随意添加它。
libgdx主项目中的Surface类(可以说是主类)
public class Surface implements ApplicationListener {
ActionResolver actionResolver;
SpriteBatch spriteBatch;
Texture texture;
Calculator calculator;
public Surface(ActionResolver actionResolver) {
this.actionResolver = actionResolver;
}
@Override
public void create() {
spriteBatch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("ship.png"));
calculator = new Calculator(texture);
actionResolver.showToast("Tap screen to open Activity");
Gdx.input.setInputProcessor(new InputProcessor() {
@Override
public boolean touchDown(int x, int y, int pointer, int button) {
actionResolver.showToast("touchDown");
actionResolver.showMyList();
return true;
}
// overriding all other interface-methods the same way
});
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
calculator.update();
spriteBatch.begin();
calculator.draw(spriteBatch);
spriteBatch.end();
}
// Overriding resize, pause, resume, dispose without functionality
}
libgdx主项目中的ActionResolver接口
public interface ActionResolver {
public void showMyList();
public void showToast(String toastMessage);
}
在我的Android项目中实施ActionResolver界面
public class ActionResolverImpl implements ActionResolver {
Handler uiThread;
Context appContext;
public ActionResolverImpl(Context appContext) {
uiThread = new Handler();
this.appContext = appContext;
}
@Override
public void showMyList() {
appContext.startActivity(new Intent(this.appContext, MyListActivity.class));
}
@Override
public void showToast(final String toastMessage) {
uiThread.post(new Runnable() {
@Override
public void run() {
Toast.makeText(appContext, toastMessage, Toast.LENGTH_SHORT).show();
}
});
}
}
用于初始化Suface-Class的Android活动
public class AndroidActivity extends AndroidApplication {
ActionResolverImpl actionResolver;
@Override
public void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionResolver = new ActionResolverImpl(this);
initialize(new Surface(actionResolver), false);
}
}
我还在我的Surface类中实现了InputProcessor,但这不应该(并没有)产生任何差别。任何想法,我缺少什么?