public void run() { animateFrame(); } };
private void processJoystickInput(MotionEvent event, int historyPos) { // Get joystick position. // Many game pads with two joysticks report the position of the second joystick // using the Z and RZ axes so we also handle those. // In a real game, we would allow the user to configure the axes manually. float x = getCenteredAxis(event, mLastInputDevice, MotionEvent.AXIS_X, historyPos); if (x == 0) { x = getCenteredAxis(event, mLastInputDevice, MotionEvent.AXIS_HAT_X, historyPos); } if (x == 0) { x = getCenteredAxis(event, mLastInputDevice, MotionEvent.AXIS_Z, historyPos); } float y = getCenteredAxis(event, mLastInputDevice, MotionEvent.AXIS_Y, historyPos); if (y == 0) { y = getCenteredAxis(event, mLastInputDevice, MotionEvent.AXIS_HAT_Y, historyPos); } if (y == 0) { y = getCenteredAxis(event, mLastInputDevice, MotionEvent.AXIS_RZ, historyPos); } // Set the ship heading. mShip.setHeading(x, y); step(historyPos < 0 ? event.getEventTime() : event.getHistoricalEventTime(historyPos)); }
void animateFrame() { long currentStepTime = SystemClock.uptimeMillis(); step(currentStepTime); Handler handler = getHandler(); if (handler != null) { handler.postAtTime(mAnimationRunnable, currentStepTime + ANIMATION_TIME_STEP); invalidate(); } }
public GameView(Context context, AttributeSet attrs) { super(context, attrs); mRandom = new Random(); mBullets = new ArrayList<>(); mObstacles = new ArrayList<>(); setFocusable(true); setFocusableInTouchMode(true); float baseSize = getContext().getResources().getDisplayMetrics().density * 5f; float baseSpeed = baseSize * 3; mShipSize = baseSize * 3; mMaxShipThrust = baseSpeed * 0.25f; mMaxShipSpeed = baseSpeed * 12; mBulletSize = baseSize; mBulletSpeed = baseSpeed * 12; mMinObstacleSize = baseSize * 2; mMaxObstacleSize = baseSize * 12; mMinObstacleSpeed = baseSpeed; mMaxObstacleSpeed = baseSpeed * 3; }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { ensureInitialized(); break; default: if (isFireKey(keyCode)) { fire(); handled = true; step(event.getEventTime()); return true;
mLastStepTime = currentStepTime; ensureInitialized(); reset(); case 0: positionX = -size; positionY = mRandom.nextInt(getHeight()); break; case 1: positionX = getWidth() + size; positionY = mRandom.nextInt(getHeight()); break; case 2: positionX = mRandom.nextInt(getWidth()); positionY = -size; break; default: positionX = mRandom.nextInt(getWidth()); positionY = getHeight() + size; break;
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { ensureInitialized(); break; default: if (isFireKey(keyCode)) { handled = true; step(event.getEventTime()); return true;
@Override public boolean onGenericMotionEvent(MotionEvent event) { ensureInitialized(); processJoystickInput(event, i); processJoystickInput(event, -1); return true;
private void crash() { getVibrator().vibrate(new long[] { 0, 20, 20, 40, 40, 80, 40, 300 }, -1); }
private Vibrator getVibrator() { if (mLastInputDevice != null) { Vibrator vibrator = mLastInputDevice.getVibrator(); if (vibrator.hasVibrator()) { return vibrator; } } return (Vibrator)getContext().getSystemService(Context.VIBRATOR_SERVICE); }
@Override public void onWindowFocusChanged(boolean hasWindowFocus) { // Turn on and off animations based on the window focus. // Alternately, we could update the game state using the Activity onResume() // and onPause() lifecycle events. if (hasWindowFocus) { getHandler().postDelayed(mAnimationRunnable, ANIMATION_TIME_STEP); mLastStepTime = SystemClock.uptimeMillis(); } else { getHandler().removeCallbacks(mAnimationRunnable); mDPadState = 0; if (mShip != null) { mShip.setHeading(0, 0); mShip.setVelocity(0, 0); } } super.onWindowFocusChanged(hasWindowFocus); }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { ensureInitialized(); break; default: if (isFireKey(keyCode)) { fire(); handled = true; step(event.getEventTime()); return true;
mLastStepTime = currentStepTime; ensureInitialized(); reset(); case 0: positionX = -size; positionY = mRandom.nextInt(getHeight()); break; case 1: positionX = getWidth() + size; positionY = mRandom.nextInt(getHeight()); break; case 2: positionX = mRandom.nextInt(getWidth()); positionY = -size; break; default: positionX = mRandom.nextInt(getWidth()); positionY = getHeight() + size; break;
void animateFrame() { long currentStepTime = SystemClock.uptimeMillis(); step(currentStepTime); Handler handler = getHandler(); if (handler != null) { handler.postAtTime(mAnimationRunnable, currentStepTime + ANIMATION_TIME_STEP); invalidate(); } }
public GameView(Context context, AttributeSet attrs) { super(context, attrs); mRandom = new Random(); mBullets = new ArrayList<Bullet>(); mObstacles = new ArrayList<Obstacle>(); setFocusable(true); setFocusableInTouchMode(true); float baseSize = getContext().getResources().getDisplayMetrics().density * 5f; float baseSpeed = baseSize * 3; mShipSize = baseSize * 3; mMaxShipThrust = baseSpeed * 0.25f; mMaxShipSpeed = baseSpeed * 12; mBulletSize = baseSize; mBulletSpeed = baseSpeed * 12; mMinObstacleSize = baseSize * 2; mMaxObstacleSize = baseSize * 12; mMinObstacleSpeed = baseSpeed; mMaxObstacleSpeed = baseSpeed * 3; }
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { ensureInitialized(); break; default: if (isFireKey(keyCode)) { handled = true; step(event.getEventTime()); return true;
@Override public boolean onGenericMotionEvent(MotionEvent event) { ensureInitialized(); processJoystickInput(event, i); processJoystickInput(event, -1); return true;