This is a fork from https://github.com/hanjoes/SKSpriteButton which reworks the state managemnet and event listeners. It also adds support for toggling buttons.
SKSpriteButton will have one of these states:
State | Description |
---|---|
.normal |
Button is initialized to this state |
.tapped |
On touch down event |
.disabled |
Button doesn't respond to user interaction |
.toggledOn |
When toggle mode is true, the button will flip between normal and toggledOn state on each touch up event |
You can set color, texture and eventListeners for any the button states. There can only be one color and/or texture for each state but ma have one or more eventListener for each state.
Set color(SKColor, forState: StateType)
property for a different display color for a given state.
Since this widget inherits from SKSpriteNode, which will only show texture if both texture
and color
are set. Color
will not be shown if the button has a texture.
Set texture(SKTexture, forState: StateType)
property for displaying a different texture for a given state.
Set isToggleMode(Bool)
to enable toggling on button touchUp events.
Set ToggledOnState(Bool)
to pre-load a toggle state
Add/Remove EventListener(SKSpriteButtonEventListener)
register and de-register an event listener
Supported Events |
---|
.touchesMoved |
.touchesEnded |
.touchesCanceled |
.touchesToggledOn |
.touchesToggledOff |
Initialize an event SKSpriteButtonEventListener(handler: EventHandler, forEvent: EventType)
let button = SKSpriteButton(imageNamed:"InActive")
button.setTexture(SKTexture(imageNamed: "Active"), forState: .toggledOn)
button.isToggleMode = true
button.setToggledOnState(true)
button.addEventListener(SKSpriteButtonEventListener(handler: self.showTab, forEvent: .touchesToggledOn))
Note: EventHandler is a reference to a method call passed to a button, eg. self.fireBigFlippingGun
or character.jumpUp
Event handler method signature will require touches, event and the button itself to the method associated with the specific event.
func showTab(touches: Set<UITouch>, event: UIEvent?, target: SKSpriteButton) {
guard let userData = target.userData else {
return
}
target.setDisabledState(true)
// do something with the user data
// do some game code
}
You can group a set of buttons together so that when a single button it tapped, it toggles on and toggles off the remaining buttons.
let toggleGroup = SKSpriteButtonGroup()
toggleGroup.addButton(button1)
toggleGroup.addButton(button2)
Note: Ensure buttons are in their correct toggle state when adding them to the group by using setToggledOnState(Bool)
Set moveType
property for different move types supported move types are:
public enum MoveType {
case alwaysHeld
case releaseOut
case releaseFast
case reentry
}
The names should be self-explainatory. But still I put a screencast for different move type behaviors.
Just copy the SKSpriteButton/Classes folder into your project and you are good to go.
Nader Eloshaiker, https://github.com/nader-eloshaiker
SKSpriteButton is available under the MIT license. See the LICENSE file for more info.
hanjoes
nader-eloshaiker