Created
March 5, 2019 11:22
-
-
Save EliteIntegrity/f4323afe65098f877640e2bd4d8486e4 to your computer and use it in GitHub Desktop.
Code the onTouchEvent function which enables the player to un-pause the game loop, move the ship and shoot a bullet at the invaders.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The SurfaceView class implements onTouchListener | |
// So we can override this method and detect screen touches. | |
override fun onTouchEvent(motionEvent: MotionEvent): Boolean { | |
when (motionEvent.action and MotionEvent.ACTION_MASK) { | |
// Player has touched the screen | |
// Or moved their finger while touching screen | |
MotionEvent.ACTION_POINTER_DOWN, | |
MotionEvent.ACTION_DOWN, | |
MotionEvent.ACTION_MOVE-> { | |
paused = false | |
if (motionEvent.y > size.y - size.y / 8) { | |
if (motionEvent.x > size.x / 2) { | |
playerShip.moving = PlayerShip.right | |
} else { | |
playerShip.moving = PlayerShip.left | |
} | |
} | |
if (motionEvent.y < size.y - size.y / 8) { | |
// Shots fired | |
if (playerBullet.shoot( | |
playerShip.position.left + playerShip.width / 2f, | |
playerShip.position.top, | |
playerBullet.up)) { | |
soundPlayer.playSound(SoundPlayer.shootID) | |
} | |
} | |
} | |
// Player has removed finger from screen | |
MotionEvent.ACTION_POINTER_UP, | |
MotionEvent.ACTION_UP -> { | |
if (motionEvent.y > size.y - size.y / 10) { | |
playerShip.moving = PlayerShip.stopped | |
} | |
} | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment