0

我正在尝试在 Android 上运行一个简单的 libgdx 项目。一切都很好,但我的 InputProcessor 不会触发它的事件。我根据本教程实现了所有内容: http ://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements3TierProjectSetup#Code_Example

“showToast”的第一次调用工作正常,并显示在我的屏幕上 => showToast-Method 确实有效。不幸的是,我无法触发任何 InputProcessor 事件。甚至调试器也不会止步于此,因此绝对不会调用它们。

编辑:这是完整的代码。我只省略了计算器类,因为它工作正常,在这里不应该有任何问题。当然,如果有人不同意,我可以随时添加。

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 Activity

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,但这不应该(也没有)有任何区别。任何想法,我错过了什么?

4

0 回答 0