SHARE
TWEET

BasicInputManager (Android)

ulfben Feb 15th, 2017 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class BasicInputManager extends InputManager
  2.         implements View.OnTouchListener {
  3.    
  4.     BasicInputManager(View view){
  5.         view.findViewById(R.id.keypad_up).setOnTouchListener(this);
  6.         view.findViewById(R.id.keypad_down).setOnTouchListener(this);
  7.         view.findViewById(R.id.keypad_left).setOnTouchListener(this);
  8.         view.findViewById(R.id.keypad_right).setOnTouchListener(this);
  9.         view.findViewById(R.id.keypad_jump).setOnTouchListener(this);
  10.     }
  11.    
  12.     @Override
  13.     public boolean onTouch(final View v, final MotionEvent event) {
  14.         int action = event.getActionMasked();
  15.         int id = v.getId();
  16.         if(action == MotionEvent.ACTION_DOWN){// User started pressing a key
  17.             if(id == R.id.keypad_up){
  18.                 mVerticalFactor -= 1;
  19.             }else if (id == R.id.keypad_down) {
  20.                 mVerticalFactor += 1;
  21.             }
  22.             if (id == R.id.keypad_left) {
  23.                 mHorizontalFactor -= 1;
  24.             } else if(id == R.id.keypad_right) {
  25.                 mHorizontalFactor += 1;
  26.             }
  27.             if (id == R.id.keypad_jump) {
  28.                 mIsJumping = true;
  29.             }
  30.         } else if(action == MotionEvent.ACTION_UP) {
  31.             if (id == R.id.keypad_up) {
  32.                 mVerticalFactor += 1;
  33.             } else if (id == R.id.keypad_down) {
  34.                 mVerticalFactor -= 1;
  35.             }
  36.             if (id == R.id.keypad_left) {
  37.                 mHorizontalFactor += 1;
  38.             } else if (id == R.id.keypad_right) {
  39.                 mHorizontalFactor -= 1;
  40.             }
  41.             if (id == R.id.keypad_jump) {
  42.                 mIsJumping = false;
  43.             }
  44.         }
  45.         return false;
  46.     }
  47. }
RAW Paste Data
Want to get better at Java?
Learn to code Java in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top