按下触摸的Android MotionEvent?
我刚刚开始在Android上开发,我正在尝试制作一个非常基本的游戏,在这个游戏中,你必须沿着屏幕底部移动一只蝙蝠,在躲避炸弹的同时捕捉物品 我遇到的问题是,当你将手指放在屏幕的左侧或右侧时,我希望球棒沿着屏幕底部移动 目前,当用户触摸屏幕时,我可以让蝙蝠移动几个像素,但在用户将手指从屏幕上移开之前,我无法让蝙蝠保持移动 以下是我迄今为止最基本的代码:按下触摸的Android MotionEvent?,android,Android,我刚刚开始在Android上开发,我正在尝试制作一个非常基本的游戏,在这个游戏中,你必须沿着屏幕底部移动一只蝙蝠,在躲避炸弹的同时捕捉物品 我遇到的问题是,当你将手指放在屏幕的左侧或右侧时,我希望球棒沿着屏幕底部移动 目前,当用户触摸屏幕时,我可以让蝙蝠移动几个像素,但在用户将手指从屏幕上移开之前,我无法让蝙蝠保持移动 以下是我迄今为止最基本的代码: package com.mattdrewery.supercatch; import android.view.View; import and
package com.mattdrewery.supercatch;
import android.view.View;
import android.view.MotionEvent;
import android.content.Context;
import android.graphics.Canvas;
public class GameView extends View
{
private Catcher catcher;
public GameView(Context context)
{
super(context);
setFocusable(true);
// Create the catcher
catcher = new Catcher(context, R.drawable.catcher, 240, 250);
}
@Override
protected void onDraw(Canvas canvas)
{
// Draw the catcher to the canvas
canvas.drawBitmap(catcher.getImage(), catcher.getPosX(), catcher.getPosY(), null);
// Redraw the screen
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
// Get the action from the touch screen
int eventAction = event.getAction();
int X = (int) event.getX();
int Y = (int) event.getY();
// If the user presses on the screen....
if (eventAction == MotionEvent.ACTION_DOWN)
{
catcher.moveLeft();
}
// Redraw the screen
invalidate();
return true;
}
}
catcher.moveLeft方法如下所示:
public void moveLeft()
{
posX -= 5;
}
在此问题上的任何帮助都将不胜感激!: ZoomControl使用,它可以做你想做的事情。它使用postDelayed并检查按钮是否仍被按下以重复onClick操作。查看源代码,看看它是如何工作的。我没有深入了解,我的只是一个提示。ZoomControl使用它,它可以做你想做的事情。它使用postDelayed并检查按钮是否仍被按下以重复onClick操作。查看源代码,看看它是如何工作的。我没有深入了解,我的只是一个提示。尝试为onTouchEvent返回false。从文档中,如果事件被处理,则返回True,否则返回false。我认为这将对您有所帮助。尝试为onTouchEvent返回false。从文档中,如果事件被处理,则返回True,否则返回false。我想这会对你有所帮助。我想这可能会奏效:
boolean actionUpFlag = false;
if (eventAction == MotionEvent.ACTION_DOWN)
{
actionUpFlag = true;
}
else if (eventAction == MotionEvent.ACTION_UP)
{
actionUpFlag = false;
}
while (actionUpFlag)
{
catcher.moveLeft();
}
这就是你要找的吗?我想这可能会奏效:
boolean actionUpFlag = false;
if (eventAction == MotionEvent.ACTION_DOWN)
{
actionUpFlag = true;
}
else if (eventAction == MotionEvent.ACTION_UP)
{
actionUpFlag = false;
}
while (actionUpFlag)
{
catcher.moveLeft();
}
这就是你要找的吗?好主意和坏链接。改用。好主意和坏链接。改用。