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.
It looks to me like you need to let
AllViewsknow that you received the information from the touch.You could create a couple of properties in
AllViewsand then in your
onTouchmethod add these lines before thereturnstatement:and finally, in your
onDrawmethod, changeto