Skip to content

This repository provides a D-Pad Controller that could be quickly added to a SpriteKit game for handling key actions.

License

Notifications You must be signed in to change notification settings

mak-s/MKGameController

Repository files navigation

MKGameController

A simple Direction pad touch controller for SpriteKit games.

A D-Pad style touch controller overlayed on a scene

How to use?

  • Instantiate an instance of TouchControllerNode and add it to the camera node in your scene.

    let controllerNode = TouchControllerNode(frame: ...)
    scene.camera.addChild(controllerNode)
  • TouchControllerNode handles the layout for D-Pad Direction and Action keys.

    Assign a delegate to TouchControllerNode's inputDelegate property.

    controllerNode.inputDelegate = objectThatConformsToInputDelegate
  • When a key action occurs, a command is sent to the delegate.

    Handle the command, to perform actions according to your game logic.

    func handle(command: InputCommand) {
        switch command {
        case .hold(let inputKey):
            // input key pressed
            ...
            
        case .cancel(let inputKey):
            // input key press cancelled
            ...
            
        case .release(let inputKey):
            // input key released
            ...
        }
    }
  • Supported Keys are defined in InputKey

  • Supported Actions are defined in InputCommand

  • Images for D-pad Keys are present in Images.xcassets.


Note:

  • Enable multiple touches on SKView that will present the D-Pad controller node.

    It is important to enable multiple touch on the SKView that will present the scene to whose camera the D-Pad controller node will be added. Else multiple touch events will not be delivered when two (or more) buttons are pressed simultaneously

    camera.scene.view.isMultipleTouchEnabled = true

About

This repository provides a D-Pad Controller that could be quickly added to a SpriteKit game for handling key actions.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages