June 2018
Intermediate to advanced
310 pages
6h 32m
English
So, how do higher-order functions help us here?
First, we'll define a namespace for all our weapons. This is not mandatory, but helps to keep everything in check:
object Weapons { // Functions we'll be there soon}
Then, instead of classes, each of our weapons will become a function:
val peashooter = fun(x: Int, y: Int, direction: Direction): Projectile { // Fly straight}val banana = fun(x: Int, y: Int, direction: Direction): Projectile { // Return when you hit screen border}val pomegranate = fun(x: Int, y: Int, direction: Direction): Projectile { // Explode when you hit first enemy}
The most interesting part is our hero. It now holds two functions:
class OurHero { // As before var currentWeapon = Weapons.peashooter val ...
Read now
Unlock full access