1

我正在创建基于 AndEngine 动态壁纸扩展的动态壁纸。在 onCreateScene() 函数中,我将触摸事件设置为我的场景。这是代码:

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) {
  mEngine.registerUpdateHandler(new FPSLogger());

  parallaxBackground = new ParallaxBackground(0, 0, 0);
  parallaxBackground.attachParallaxEntity(new ParallaxEntity(1.0f, mySprite));
  mCurrentScene.setBackground(parallaxBackground);

  mCurrentScene.setOnSceneTouchListener(this);

  pOnCreateSceneCallback.onCreateSceneFinished(mCurrentScene);
}

之后我创建 onSceneTouchEvent() 函数:

@Override
public boolean onSceneTouchEvent(Scene scene, TouchEvent event) {
switch(event.getAction()){
    case TouchEvent.ACTION_DOWN:                    
        Log.i("Logged TouchEvent DOWN", ""+event.getAction());
        break;
    case TouchEvent.ACTION_MOVE:
        Log.i("Logged TouchEvent MOVE", ""+event.getAction());
        break;
    case TouchEvent.ACTION_UP:
        Log.i("Logged TouchEvent UP", ""+event.getAction());
        break;
}
return true;        
}

这段代码一切正常吗?好的。我在我的手机(三星 Galaxy S III mini)上运行它......,当我第一次触摸屏幕时,日志中写着:

AndEngine                     org.andengine.input.touch.TouchEvent$TouchEventPool<TouchEvent> was exhausted, with 0 item not yet recycled. Allocated 1 more.
Logged TouchEvent DOWN        0

当我第二次触摸屏幕等时,日志中写着:

Logged TouchEvent DOWN        0
Logged TouchEvent DOWN        0
Logged TouchEvent DOWN        0
Logged TouchEvent DOWN        0
.....

...仅检测到 ACTION_DOWN!ACTION_UP 和 ACTION_MOVE 不起作用!

也许所有触摸事件在 AndEngine 动态壁纸扩展中都不可用?谁知道?如何解决这个问题呢?

4

1 回答 1

1

我通过重新实现 BaseWallpaperGLEngine 类来解决它。

在您的 LiveWallpaperService 中插入以下代码:

@Override
public Engine onCreateEngine() {
    return new LiveWallpaperEngine(this);
}

public class LiveWallpaperEngine extends BaseWallpaperGLEngine {

    public LiveWallpaperEngine(IRendererListener pRendererListener) {
        super(pRendererListener);
    }

    @Override
    public void onTouchEvent(MotionEvent event) {
        mEngine.onTouch(null, MotionEvent.obtain(event));
    }
}

@Override
protected void onTap(int pX, int pY) {
}

我希望这可以帮助有同样问题的人;)

于 2014-09-10T22:15:45.610 回答