所以我想做的是一个小太空游戏,你用一个图形屏幕操纵杆控制飞船,使用touch
和drag
。我已经让这个工作正常了。一旦我开始尝试添加触摸屏幕顶部以发射武器的能力,问题就出现了。出于某种原因,如果您当前正在拖动操纵杆,它似乎会忽略其他触摸输入并且不执行任何操作(但是一旦我停止按住 ,它就可以正常工作joystick
)。这是我第一次使用java,Android
所以它可能很愚蠢,但我已经坚持了几天。无论如何,这是我的代码。
以下来自我的
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
@Override
public boolean onTouchEvent(MotionEvent event) {
fingers= event.getPointerCount(); //Returns 1 or 2 properly if 2 fingers on screen
playership.onTouchEvent(event); //Pass the event along to the spaceship object
return true;
//return super.onTouchEvent(event); //no idea what this does, but it seems to disable the drag event
}
下面是 playership.onTouchEvent(event); 功能
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
if(event.getY()<(Panel.mHeight-150))
{
Laser1.reset(mX,mY,(int)event.getX(),(int)event.getY());
}
break;
}
joy1.onTouchEvent(event);
return true;
}
下面是joy1.onTouchEvent(event);
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
//Offset between mouse(x,y) and center
double offx=oX-event.getX();
double offy=oY-event.getY();
double d=Math.sqrt((offx*offx)+(offy*offy));
switch (eventaction )
{
case MotionEvent.ACTION_DOWN:
if(d<dragrange) //Start Dragging if clicked
{
offx=offx/d;
offy=offy/d;
Dragging=true;
reset = false;
dX=(int) (offx*dragrange);
dY=(int) (offy*dragrange);
}
break;
case MotionEvent.ACTION_MOVE:
if(Dragging)//if joy is already being draged, update it.
{
offx=offx/d;
offy=offy/d;
if(d>dragrange)
{
dX=(int) (offx*dragrange);
dY=(int) (offy*dragrange);
}
else
{
dX=(int) (offx*d);
dY=(int) (offy*d);
}
reset = false;
}
break;
case MotionEvent.ACTION_UP:
if(Dragging)
{
Dragging=false;
}
break;
}
return true;
}
所以,是的,问题在于,如果操纵杆在箱子中间,MotionEvent.ACTION_MOVE
它似乎会阻止任何其他触摸事件正确注册。