xcodeaccelerometerd-pad

Swapping between control schemes mid game


Right, I am coding a game where you must navigate a maze using either a D-Pad or the device's accelerometer. I have successfully coded both methods, but how can I use an in game 'switch' to swap between the two?


Solution

  • Use boolean flags to toggle which controls should work.

    Some pseudo-Swift code for an example:

    Bool shouldUseDPad
    
    func changeControlScheme() {
        if shouldUseDPad {
            shouldUseDPad = false
        } else {
            shouldUseDPad = true
        }
    }
    
    func theDPadListener(dPadEvent) {
        if shouldUseDPad {
            doStuff(dPadEvent)
        }
    }
    
    func theAccelerometerListener(accelEvent) {
        if !shouldUseDPad {
            doStuff(accelEvent)
        }
    }