我正在进行简单的乒乓球游戏,而我正试图让矩形顶部停止在屏幕结束时向左或向右移动。我尝试在MotionEvent部分中使用少量Ifs来做到这一点,但它不起作用,它就像没有任何Ifs一样离开屏幕:D
我正在使用它来获取屏幕大小:
Point size = new Point();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
display.getSize(size);
canvasWidth = size.x;
canvasHeight = size.y;
MotionEvent检测:
public boolean doTouchEvent(MotionEvent event){
synchronized(mSurfaceHolder){
switch(event.getAction()){
case MotionEvent.ACTION_MOVE:{
xTouch = (int)event.getX();
yTouch = (int)event.getY();
rectLeft = (float)xTouch-200;
rectTop = (float)yTouch+25;
rectRight = (float)xTouch+200;
rectBot = (float)yTouch-25;
if(rectLeft <= 0)
rectLeft = 0;
if(rectBot >= canvasWidth)
rectBot = canvasWidth;
}
}
}
}
绘制矩形:
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
super.draw(canvas);
if(firstClick){
//canvas.drawBitmap(img, xTouch - (imgWidth/2), yTouch - (imgHeight/2), null);
canvas.drawRect(rectLeft, rectTop, rectRight, rectBot, paint);
}
}
答案 0 :(得分:0)
在您的代码中:
if(rectLeft <= 0)
rectLeft = 0;
我想你忘记了设置rectRight。也许试试:
if(rectLeft <= 0) {
rectLeft = 0;
rectRight = 200;
}
或类似的东西。