需要帮助,我想为我的学校制作一个安卓游戏作为项目。我尝试使用 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。