触摸按钮andengine android上的监听器

时间:2014-10-21 14:11:05

标签: android button andengine

我正在开发游戏,我在sprite上注册触摸事件以用作按钮,我得到以下错误可以帮助解决这个问题。

10-21 19:03:22.895: E/AndroidRuntime(17410): FATAL EXCEPTION: UpdateThread
    10-21 19:03:22.895: E/AndroidRuntime(17410): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at android.os.Handler.<init>(Handler.java:121)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at android.widget.Toast$TN.<init>(Toast.java:317)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at android.widget.Toast.<init>(Toast.java:91)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at android.widget.Toast.makeText(Toast.java:233)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at test.matim.Level2GameScene$1.onAreaTouched(Level2GameScene.java:90)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.entity.scene.Scene.onAreaTouchEvent(Scene.java:409)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.entity.scene.Scene.onSceneTouchEvent(Scene.java:357)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.engine.Engine.onTouchScene(Engine.java:452)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.engine.Engine.onTouchEvent(Engine.java:438)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.input.touch.controller.BaseTouchController$TouchEventRunnablePoolItem.run(BaseTouchController.java:102)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.util.adt.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:54)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.util.adt.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:1)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.util.adt.pool.PoolUpdateHandler.onUpdate(PoolUpdateHandler.java:88)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.input.touch.controller.BaseTouchController.onUpdate(BaseTouchController.java:62)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.engine.Engine.onUpdate(Engine.java:584)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.engine.Engine.onTickUpdate(Engine.java:548)
    10-21 19:03:22.895: E/AndroidRuntime(17410):    at org.andengine.engine.Engine$UpdateThread.run(Engine.java:820)

我在这里如何做到这一点:

button = new Sprite(25, 310, activity.exitButtonRegion, BaseActivity.getSharedInstance().getVertexBufferObjectManager())
 {

@Override
 protected void onManagedUpdate(float pSecondsElapsed) {

     super.onManagedUpdate(pSecondsElapsed);
}

@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
             float pTouchAreaLocalX, float pTouchAreaLocalY) {

     if(pSceneTouchEvent.isActionDown())
     {
         Toast.makeText(Level2GameScene.this.activity, "Clicked", Toast.LENGTH_LONG).show();
     }
     return true;
}
};
this.registerTouchArea(button); 
 attachChild(button);
    }

1 个答案:

答案 0 :(得分:0)

你应该像那样显示Toast,你应该在UiThread中进行调用

this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(Level2GameScene.this.activity, "Clicked", Toast.LENGTH_LONG).show();
        }
    });