Spencer Stepanek Spencer Stepanek - 11 months ago 62
Swift Question

Creating a button using SKLabelNode as a parent in Swift SpriteKit

I'm trying to create a subclass of SKLabelNode for a button in SpriteKit. I tried to create a button using SKLabelNode as a parent, so I can use everything I know about labels while creating my buttons (font, text size, text color, position, etc).

I've looked into Swift Spritekit Adding Button Programaticly and I'm using the basis of what that is saying, but rather I'm making a subclass instead of a variable, and I'm creating the button using a label's code. The subclass has the added function that will allow it to be tapped and trigger an action.

class StartScene: SKScene {

class myButton: SKLabelNode {

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if myButton.containsPoint(location) {

// Action code here

override func didMoveToView(view: SKView) {

let startGameBtn = myButton(fontNamed: "Copperplate-Light")
startGameBtn.text = "Start Game"
startGameBtn.fontColor = SKColor.blackColor()
startGameBtn.fontSize = 42
startGameBtn.position = CGPointMake(frame.size.width/2, frame.size.height * 0.5)


My error is in: if myButton.containsPoint(location)

The error reads: Cannot invoke 'containsPoint' with an argument list of type '(CGPoint)'

I know it has to do something with my subclass, but I have no idea what it is specifically.

I also tried putting parentheses around myButton.containsPoint(location) like so:

if (myButton.containsPoint(location))

But then the error reads: 'CGPoint' is not convertible to 'SKNode'


Answer Source

I have an alternative solution that would work the same way. Change the following code

if myButton.containsPoint(location) {

To this and it should compile and have the same functionality

if self.position == location {