這很容易。按照以下代碼
舉例說明實體。代碼是自描述性的。
public class Example extends Entity implements IOnAreaTouchListener {
public Rectangle aSprite;
public Example(float pX, float pY,
VertexBufferObjectManager pVertexBufferObjectManager) {
// initialize your sprite & it's touch implementation
aSprite = new Rectangle(pX, pY, 40, 40, pVertexBufferObjectManager) {
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
if (pSceneTouchEvent.isActionUp()) {
Log.e("Rectangle aSprite", "isActionUp");
return true;
}
return false;
}
};
}
@Override
public void onAttached() {
super.onAttached();
}
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
ITouchArea pTouchArea, float pTouchAreaLocalX,
float pTouchAreaLocalY) {
aSprite.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
pTouchAreaLocalY);
return false;
}
}
在你自定義場景類或者onCreateScene(),寫這樣的
Example example = new Example(80, 80, getVertexBufferObjectManager());
mCurrentScene.attachChild(example.aSprite);
mCurrentScene.registerTouchArea(example.aSprite);