onTouchEvent得到两个输入并向玩家移动一个方向,如果它卡住了

时间:2014-07-16 19:46:50

标签: java

@Override
public boolean onTouchEvent(MotionEvent event)
{
    if (event.getAction() == MotionEvent.ACTION_DOWN) 
    {
        switch (state)
        {
        case Ready:
        {
            state = GameState.Running;
            break;
        }
        case GameOver:
        {
            state=GameState.Ready;
            plane.getBombs().clear();
            soldier.setAlive(true);
            break;
        }
        case Paused:
        {
            state = GameState.Running;
            break;
        }
        case Running:
        {
            if (event.getX() < screenWidth/2)
            {
                soldier.moveLeft();
                soldier.setMovingLeft(true);
            }
            else if (event.getX() > screenWidth/2 ) 
            {
                soldier.moveRight();
                soldier.setMovingRight(true);
            }
        }
            break;
        default:
            break;

        }

    }

    else if (event.getAction() == MotionEvent.ACTION_UP) 
    {
        if (event.getX() < screenWidth/2)
        {
            soldier.stopLeft();
        }
         if (event.getX() > screenWidth/2) 
        {
            soldier.stopRight();
        }
    }

    return true;
}

如果我用一根手指握住并触摸右侧球员移动很好但触摸右侧并且我向左触摸并释放球员向右移动直到我触摸右侧

1 个答案:

答案 0 :(得分:0)

尝试更改:

public void moveLeft() { speedX = -moveSpeed; } 

到:

public void moveLeft() { speedX -= moveSpeed; }