简体   繁体   English

Android onTouchListener

[英]Android onTouchListener

Need help, I am tring to make an android game as project for my school. 需要帮助,我正准备将android游戏作为我学校的项目。 I try to use ontouchListener in order to bump the mole. 我尝试使用ontouchListener来碰碰痣。

Main Activity 主要活动

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;
}

All views class 所有意见班

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();     

}

I have also class to mole, points,time and the stage screen. 我也有班级来骚扰,点数,时间和舞台屏幕。 but I don't know in which view to make the ontouchlistener. 但我不知道用哪种视图制作触摸监听器。 for a try I made a blue circle and try to change it's place with the new positions from the listener event but no lack. 尝试了一下,我做了一个蓝色的圆圈,并尝试用侦听器事件中的新位置更改了它的位置,但并不缺少。

any help will be great. 任何帮助都会很棒。 thanks, 谢谢,

cfir. cfir。

The onTouchListener should be implemented in the AllViews class, since if you implement it in the other views, you'll get the position relative to left/top of the specific view. onTouchListener应该在AllViews类中实现,因为如果在其他视图中实现它,则将获得相对于特定视图的左/上位置的位置。

in the onDraw of AllViews, you can draw the rest of your objects 在AllViews的onDraw中,您可以绘制其余对象

In which view you want to make an onTouchListener is up to you, but that onTouchListener is an interface which need to be implemented. 您要在哪种视图中创建onTouchListener取决于您,但是onTouchListener是需要实现的接口。 And when implemented, you'll need the right method, which in this case I believe is onTouch() . 并且在实现后,您将需要正确的方法,在这种情况下,我认为是onTouch() Have a look at the documentation 看一下文档

It looks to me like you need to let AllViews know that you received the information from the touch. 在我看来,您需要让AllViews知道您从触摸屏上收到了信息。

You could create a couple of properties in AllViews 您可以在AllViews创建几个属性

private int touchX;
private int touchY;

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

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

and then in your onTouch method add these lines before the return statement: 然后在您的onTouch方法中,在return语句之前添加以下行:

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

and finally, in your onDraw method, change 最后,在您的onDraw方法中,进行更改

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

to

canvas.drawCircle(this.touchX, this.touchY, 40, test);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM