Reputation: 439
Need help, I am tring to make an android game as project for my school. I try to use ontouchListener in order to bump the mole.
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.
Upvotes: 0
Views: 2215
Reputation: 1131
It looks to me like you need to let AllViews
know that you received the information from the touch.
You could create a couple of properties in 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:
AllViews.setTouchX(x);
AllViews.setTouchY(y);
and finally, in your onDraw
method, change
canvas.drawCircle(first.getX(), first.getY(), 40, test);
to
canvas.drawCircle(this.touchX, this.touchY, 40, test);
Upvotes: 1
Reputation: 1961
In which view you want to make an onTouchListener
is up to you, but that onTouchListener
is an interface which need to be implemented. And when implemented, you'll need the right method, which in this case I believe is onTouch()
. Have a look at the documentation
Upvotes: 1
Reputation: 1035
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.
in the onDraw of AllViews, you can draw the rest of your objects
Upvotes: 1