Instantly share code, notes, and snippets.
Created
November 8, 2015 22:47
-
Save richy486/5d408c442ac1c0c2891f to your computer and use it in GitHub Desktop.
Button node for Sprite kit in swift 2.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Button node for Sprite kit in swift 2.1 | |
// http://stackoverflow.com/questions/19082202/setting-up-buttons-in-skscene | |
import Foundation | |
import SpriteKit | |
class FTButtonNode: SKSpriteNode { | |
enum FTButtonActionType: Int { | |
case TouchUpInside = 1, | |
TouchDown, TouchUp | |
} | |
var isEnabled: Bool = true { | |
didSet { | |
if (disabledTexture != nil) { | |
texture = isEnabled ? defaultTexture : disabledTexture | |
} | |
} | |
} | |
var isSelected: Bool = false { | |
didSet { | |
texture = isSelected ? selectedTexture : defaultTexture | |
} | |
} | |
var defaultTexture: SKTexture | |
var selectedTexture: SKTexture | |
var label: SKLabelNode | |
required init(coder: NSCoder) { | |
fatalError("NSCoding not supported") | |
} | |
init(normalTexture defaultTexture: SKTexture!, selectedTexture:SKTexture!, disabledTexture: SKTexture?) { | |
self.defaultTexture = defaultTexture | |
self.selectedTexture = selectedTexture | |
self.disabledTexture = disabledTexture | |
self.label = SKLabelNode(fontNamed: "Helvetica"); | |
super.init(texture: defaultTexture, color: UIColor.whiteColor(), size: defaultTexture.size()) | |
userInteractionEnabled = true | |
//Creating and adding a blank label, centered on the button | |
self.label.verticalAlignmentMode = SKLabelVerticalAlignmentMode.Center; | |
self.label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center; | |
addChild(self.label) | |
// Adding this node as an empty layer. Without it the touch functions are not being called | |
// The reason for this is unknown when this was implemented...? | |
let bugFixLayerNode = SKSpriteNode(texture: nil, color: UIColor.clearColor(), size: defaultTexture.size()) | |
bugFixLayerNode.position = self.position | |
addChild(bugFixLayerNode) | |
} | |
/** | |
* Taking a target object and adding an action that is triggered by a button event. | |
*/ | |
func setButtonAction(target: AnyObject, triggerEvent event:FTButtonActionType, action:Selector) { | |
switch (event) { | |
case .TouchUpInside: | |
targetTouchUpInside = target | |
actionTouchUpInside = action | |
case .TouchDown: | |
targetTouchDown = target | |
actionTouchDown = action | |
case .TouchUp: | |
targetTouchUp = target | |
actionTouchUp = action | |
} | |
} | |
/* | |
New function for setting text. Calling function multiple times does | |
not create a ton of new labels, just updates existing label. | |
You can set the title, font type and font size with this function | |
*/ | |
func setButtonLabel(title: NSString, font: String, fontSize: CGFloat) { | |
self.label.text = title as String | |
self.label.fontSize = fontSize | |
self.label.fontName = font | |
} | |
var disabledTexture: SKTexture? | |
var actionTouchUpInside: Selector? | |
var actionTouchUp: Selector? | |
var actionTouchDown: Selector? | |
weak var targetTouchUpInside: AnyObject? | |
weak var targetTouchUp: AnyObject? | |
weak var targetTouchDown: AnyObject? | |
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { | |
if (!isEnabled) { | |
return | |
} | |
isSelected = true | |
if (targetTouchDown != nil && targetTouchDown!.respondsToSelector(actionTouchDown!)) { | |
UIApplication.sharedApplication().sendAction(actionTouchDown!, to: targetTouchDown, from: self, forEvent: nil) | |
} | |
} | |
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { | |
if (!isEnabled) { | |
return | |
} | |
let touch: AnyObject! = touches.first | |
let touchLocation = touch.locationInNode(parent!) | |
if (CGRectContainsPoint(frame, touchLocation)) { | |
isSelected = true | |
} else { | |
isSelected = false | |
} | |
} | |
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { | |
if (!isEnabled) { | |
return | |
} | |
isSelected = false | |
if (targetTouchUpInside != nil && targetTouchUpInside!.respondsToSelector(actionTouchUpInside!)) { | |
let touch: AnyObject! = touches.first | |
let touchLocation = touch.locationInNode(parent!) | |
if (CGRectContainsPoint(frame, touchLocation) ) { | |
UIApplication.sharedApplication().sendAction(actionTouchUpInside!, to: targetTouchUpInside, from: self, forEvent: nil) | |
} | |
} | |
if (targetTouchUp != nil && targetTouchUp!.respondsToSelector(actionTouchUp!)) { | |
UIApplication.sharedApplication().sendAction(actionTouchUp!, to: targetTouchUp, from: self, forEvent: nil) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It has a bug where I can't add three small buttons next to each other, when you touch the button that you want, it goes Im guessing to the one that has the bigger frame. I tried testing one by one and found out that when you touch outside of each button it would not work but the texture does change to the selectedTexture. and when you add up the 3 buttons because of the texture changing it wouldnt let me select the actual button i want to click. any suggestions?