SKGamePad is a simple-to-use SpriteKit framework that provides an on-screen gamepad.
SKGamePad is built from ground up with Swift and extends SKNode and SKSpriteNode. This framework adds convenience class to initializ and listen to gamepad touch event.
- Compatible with Xcode 9 and Swift 4
- Fully documented.
- Convenience class
SKColorButton: SKSpriteNode
creates retangular button that listens for touch events. - Convenience class for creating button from textures or images.
- Convenience class
SKDirectionalPad: SKNode
creates a node that contains 4 conventional direction button. - Convenience class for creating utilities button such as
A, B, X, Y
on conventional gamepad.
- iOS 9.0+
- Xcode 9.0 / Swift 4.0
- Create a Cartfile in the root of your project and add:
github "phuocpeter19/SKGamePad"
- Then run
carthage update
on Terminal.
- Download the latest release of this repo and add it to your Xcode project.
class Scene: SKScene {
...
override func didMove(to view: SKView) {
let button = SKColorButton(name: SKDirectionalPadButtons.upButton.rawValue, color: padColor, size: padSize)
button.listener = self
self.addChild(button)
}
}
extension Scene: SKColorButtonTouchesListener {
func buttonTouched(button: SKColorButton, withState state: SKColorButtonTouchEvent) {
// Do something when the button is touched.
}
}
SKDirectionalPadButtons
is a convenience enumeration for conventional d-pad button. It is recommended to userawValues
from the enum to define d-pad button.
- Implement protocol
SKColorButtonTouchesListener
method to receive notification of the button's touch events.
Remember to set the listener of the button!
class Scene: SKScene {
...
override func didMove(to view:SKView) {
let dpad = SKDirectionalPad(padColor: UIColor.blue, padSize: CGSize(width: 100, height: 100))
// This will position the dpad at the bottom left of the screen while taking care of the safe area insets of new iPhone X.
dpad.setPositionToBottomLeft(view)
dpad.delegate = self
self.addChild(dpad)
}
}
extension Scene: SKDirectionalPadDelegate {
func upPadEventTriggered(eventState state: SKColorButtonTouchEvent) {
// Do something when up button is touched.
// repeated this for other buttons.
}
}