我創建了一些小精靈,並且在另一個視圖的構造函數中使用bmp作爲背景。但是當我點擊那些有onClickListener的精靈時,我點擊的視圖就是背景視圖。onClick hierachy
一些代碼:
我的構造函數:
public ToposGameView(Context context) {
super(context);
moles = new ArrayList<MoleSprite>();
setFocusable(true);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(tag, "Click en GameView");
}
});
gameLoopThread = new GameLoopThread(this);
int id = 0;
for(int x = 0; x<3; x++){
for(int y = 0; y<4 ; y++){
MoleSprite mole = new MoleSprite(this, x*WIDTH/3, HEIGHT/6+y*HEIGHT/6, FRONT);
mole.setId(id);
id++;
moles.add(mole);
}
}
holder = getHolder();
holder.addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
boolean retry=true;
gameLoopThread.setRunning(false);
while(retry){
try{
gameLoopThread.join();
retry=false;
}catch(InterruptedException i){
}
}
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
}
雪碧的構造:
public MoleSprite(ToposGameView gameView, int x, int y, int direction) {
super(gameView.getContext()); //TODO
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bad1);
this.width = bmp.getWidth()/BMP_COLUMNS;
this.height = bmp.getHeight()/BMP_ROWS;
this.x=x;
this.y= y;
this.setClickable(true);
this.setFocusable(true);
this.setFocusableInTouchMode(true);
setOnClickListener(this);
setOnTouchListener(this);
//I don't know what else to do!
}
這是我的第一個問題,請問我要更多的信息。
EDIT1:
public class MoleSprite extends View implements OnClickListener, OnTouchListener{
public class ToposGameView extends SurfaceView{
MoleSprite的基類是什麼?您在詢問onClick,通常是指View層次結構,但是您使用SurfaceView代表視圖外部的渲染。這裏有一些斷開連接,但是太少的信息來識別它。 – Anm 2011-03-26 17:28:52
這個想法是做一個遊戲,需要重繪,但我與onClick卡住了。我用基類編輯。 – Rafaesp 2011-03-26 17:36:27