コード例 #1
0
  @Override
  public boolean onTouch(MotionEvent e) {
    switch (e.getAction()) {
      case MotionEvent.ACTION_DOWN:
        if (gamestate == State.ROUNDSUMMARY
            || gamestate == State.STARTGAME
            || gamestate == State.PLAYERDIED) {
          // Game�̏�����
          randomSpeed = 1;
          gamestate = State.STARTROUND; // prep and start round
          return false; // no followup msgs
        } else if (gamestate == State.GAMEOVER) {
          act.leaveGame(); // user touched after gameover -> back to entry screen
          return false; // no followup msgs
        } else {
          synchronized (fruitsSelectable) {
            Iterator<Fruit> itf = fruitsSelectable.iterator();
            while (itf.hasNext()) {
              Fruit f = itf.next();
              if (f.hasCollision(e.getX(), e.getY()))
                if (f.seed == ketseed) {
                  // user popped ketchup
                  act.playSound(Sound.KSPLAT);
                  f.burst();
                  loseLife();
                  return false; // no followup msgs
                } else {
                  // user picked up a fruit
                  selectedFruit = f;
                }
            }
          }
          if (mVelocityTracker == null) {
            // Retrieve a new VelocityTracker object to watch the velocity of a motion.
            mVelocityTracker = VelocityTracker.obtain();
          } else {
            // Reset the velocity tracker back to its initial state.
            mVelocityTracker.clear();
          }
          // Add a user's movement to the tracker.
          mVelocityTracker.addMovement(e);
        }
        break;

      case MotionEvent.ACTION_MOVE:
        if (selectedFruit != null) {
          selectedFruit.x = e.getX();
          selectedFruit.y = e.getY();
        }
        mVelocityTracker.addMovement(e);
        break;

      case MotionEvent.ACTION_UP:
        if (selectedFruit != null) {
          Fruit f = selectedFruit;
          selectedFruit = null;

          mVelocityTracker.computeCurrentVelocity(1000);
          int pointerId = e.getPointerId(e.getActionIndex());
          float tvx = VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId);
          float tvy = VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId);

          if (-tvy > 10) {
            // there is upward motion at release-- user threw fruit

            // scale throw speed for display size/density
            tvx = tvx / act.densityscalefactor;
            tvy = tvy / act.densityscalefactor;

            // help ease perspective problem when we release fruit away from the horizontal center
            // of the screen
            tvx += (e.getX() - width / 2) * 3.5 * act.densityscalefactor;

            f.throwFruit(tvx, tvy);
            synchronized (fruitsFlying) {
              fruitsFlying.add(f);
              fruitsSelectable.remove(f);
            }

            // attempting to adjust sound for how hard fruit was thrown horizontally.
            // hardness == 0 --> not thrown with any force
            // hardness == 1 --> thrown as hard as possible
            // assume that 5000 represents "really fast"; z vel should sound "harder" than y-vel
            float hardness = (f.vz - f.vy / 2) / 5000; // vy: up is negative.
            if (hardness >= 1f) hardness = 1.0f;
            if (hardness < .3f) hardness = .3f;
            act.playSound(Sound.THROW, hardness * .9f, hardness * 2);
          }
        }
        mVelocityTracker.recycle();
        // seems to be a bug here on android 4.4, causing IllegalStateException - not addressing,
        // since it doesn't affect game play, and may be resolved in later versions
        break;
    }

    return true;
  }