Git Product home page Git Product logo

skspritebutton's Introduction

SKSpriteButton

Info

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.

Usage

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.

Color

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.

Texture

Set texture(SKTexture, forState: StateType) property for displaying a different texture for a given state.

Toggle Support

Set isToggleMode(Bool) to enable toggling on button touchUp events. Set ToggledOnState(Bool) to pre-load a toggle state

EventListeners

Add/Remove EventListener(SKSpriteButtonEventListener) register and de-register an event listener

Event Management

EventType

Supported Events
.touchesMoved
.touchesEnded
.touchesCanceled
.touchesToggledOn
.touchesToggledOff

Creating an Event Listener

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

Creating an Event Handler

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
}

Toggle Group

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.

Example

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)

Move Types

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.

Demo

Installation

Just copy the SKSpriteButton/Classes folder into your project and you are good to go.

Author

Nader Eloshaiker, https://github.com/nader-eloshaiker

License

SKSpriteButton is available under the MIT license. See the LICENSE file for more info.

Contributors

hanjoes

nader-eloshaiker

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.