An 8-directional joystick class that sends updates to a delegate. Drag and drop to install. Does not come with assets.
I needed a joystick for a SpriteKit demo I was building and couldn't find anything online. I hope that by uploading it to github, someone else may be able to benefit from the way I implemented it - or perhaps even use it directly in their own project.
My HUD is built in UIKit and sends user input via the delegate pattern. Joystick is a subclass of UIImageView that reads user touches, determines where in itself they occured, and translates them into a direction.
The delegate is only notified when the direciton changes, so that the hero can react accordingly. If the user lets go of the joystick, the delegate receives a nil
direction. An opportunity to "idle" your hero.
.
- Drag and drop both Swift files into your project.
- Add a
UIImageView
to your storyboard and subclass it as aJoystick
. - Check the box that says
User Interaction Enabled
. (Or setisUserInteractionEnabled = true
in Swift). - Create an outlet and in
viewDidLoad()
, set your ViewController as the joystick's delegate. - Conform to
JoystickDelegate
.
Feel free to make any edits you want. (Ex. rotate a single asset instead of providing one for each direction.)