示例#1
0
  /**
   * Touching is the method of movement. Touching the touchscreen, that is. A player can join in
   * simply by touching where they would in a normal game.
   */
  public boolean onTouch(View v, MotionEvent mo) {
    if (v != this || !gameRunning()) return false;

    // We want to support multiple touch and single touch
    InputHandler handle = InputHandler.getInstance();

    // Loop through all the pointers that we detected and
    // process them as normal touch events.
    for (int i = 0; i < handle.getTouchCount(mo); i++) {
      int tx = (int) handle.getX(mo, i);
      int ty = (int) handle.getY(mo, i);

      // Bottom paddle moves when we are playing in one or two player mode and the touch
      // was in the lower quartile of the screen.
      if (mBlue.player && mBlue.inTouchbox(tx, ty)) {
        mBlue.destination = tx;
        mGreen.destination = tx;
        mCyan.destination = tx;
      } else if (mRed.player && mRed.inTouchbox(tx, ty)) {
        mRed.destination = tx;
        mYellow.destination = tx;
        mOrange.destination = tx;
      } else if (mo.getAction() == MotionEvent.ACTION_DOWN && mPauseTouchBox.contains(tx, ty)) {
        if (mCurrentState != State.Stopped) {
          mLastState = mCurrentState;
          mCurrentState = State.Stopped;
        } else {
          mCurrentState = mLastState;
          mLastState = State.Stopped;
        }
      }

      // In case a player wants to join in...
      if (mo.getAction() == MotionEvent.ACTION_DOWN) {
        if (!mBlue.player && mBlue.inTouchbox(tx, ty)) {
          mBlue.player = true;
        } else if (!mRed.player && mRed.inTouchbox(tx, ty)) {
          mRed.player = true;
        }
      }
    }

    return true;
  }