0

需要帮助,我想为我的学校制作一个安卓游戏作为项目。我尝试使用 ontouchListener 来撞击痣。

主要活动

public class First_Stage extends Activity implements OnTouchListener{

private AllViews allViews;
private MoleView moleView;
private PointsView pointsView;
private TimerView timerView;
private StageView stageView;
private Mole mole; 
private MoveMole moleMove;
private int points=0;
private StagePoints stagePoints;
private PointsSingelton poin;
private float x,y;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    mole=new Mole();
    stageView=new StageView(this);
    moleView=new MoleView(this,mole,x,y);
    pointsView=new PointsView(this);
    timerView=new TimerView(this, "3:33");

    allViews=new AllViews(this);
    allViews.setViews(stageView, moleView, pointsView, timerView);

    setContentView(allViews);
    allViews.setOnTouchListener((View.OnTouchListener)this);

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    x=event.getX();
    y=event.getY();

    return true;
}

所有视图类

public class AllViews extends SurfaceView implements SurfaceHolder.Callback, Runnable{

private MoleView moleView;
private PointsView pointsView;
private TimerView timerView;
private StageView mainView;
private float x,y;
private Paint test;
private First_Stage first;
Thread drawThread = new Thread(this);
SurfaceHolder holder;


public AllViews(Context context) {
    super(context);
    test=new Paint();
    first=new First_Stage();
    holder= getHolder();
    holder.addCallback(this);
}


public void setViews(StageView mainView, MoleView moleView, PointsView pointsView,TimerView timerView)

{
    this.mainView = mainView;
    this.moleView = moleView;
    this.pointsView = pointsView;
    this.timerView = timerView;

}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mainView.onDraw(canvas);    
    moleView.onDraw(canvas);
    pointsView.onDraw(canvas);
    timerView.onDraw(canvas);
    test.setColor(Color.BLUE);
    canvas.drawCircle(first.getX(), first.getY(), 40, test);

}



@Override
public void run() {
     Canvas c;
        while (true) {
            c = null;

            try {
                c = holder.lockCanvas(null);

                synchronized (holder) {
                    onDraw(c);

                }
            } finally {

                if (c != null) {
                    holder.unlockCanvasAndPost(c);
                }

            }
        }                   
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    drawThread.start();     

}

我也上课去痣,积分,时间和舞台屏幕。但我不知道在哪个视图中制作 ontouchlistener。为了尝试,我做了一个蓝色圆圈,并尝试用监听器事件的新位置改变它的位置,但不乏。

任何帮助都会很棒。谢谢,

cfir。

4

3 回答 3

1

onTouchListener 应该在 AllViews 类中实现,因为如果您在其他视图中实现它,您将获得相对于特定视图的左侧/顶部的位置。

在 AllViews 的 onDraw 中,您可以绘制其余的对象

于 2012-10-13T22:08:25.563 回答
1

您要在哪个视图中创建onTouchListener取决于您,但这onTouchListener是一个需要实现的接口。实施后,您将需要正确的方法,在这种情况下,我认为是onTouch(). 看看文档

于 2012-10-13T22:09:47.103 回答
1

在我看来,您需要AllViews告知您从触摸中收到了信息。

您可以在其中创建几个属性AllViews

private int touchX;
private int touchY;

public void setTouchX(int x) { this.touchX = x; }

public void setTouchY(int y) { this.touchY = y; }

然后在您的方法中在语句onTouch之前添加这些行:return

AllViews.setTouchX(x);
AllViews.setTouchY(y);

最后,在你的onDraw方法中,改变

canvas.drawCircle(first.getX(), first.getY(), 40, test);

canvas.drawCircle(this.touchX, this.touchY, 40, test);
于 2012-10-13T22:22:12.800 回答