Swift Spritekit Adding Button programmatically

Skiddyflyer

How do I programmatically add a button that will run an action when it's clicked? What code would be used?

I am used to just adding a button in the storyboard and running an IBAction from there.

Mike S

Adding a button in SpriteKit and responding to taps on it is not quite as easy as it is in UIKit. You basically need to create an SKNode of some sort which will draw your button and then check to see if touches registered in your scene are within that node's bounds.

A really simple scene with just a single red rectangle in the center acting as a button would look something like this:

class ButtonTestScene: SKScene {
    var button: SKNode! = nil

    override func didMoveToView(view: SKView) {
        // Create a simple red rectangle that's 100x44
        button = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 100, height: 44))
        // Put it in the center of the scene
        button.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

        self.addChild(button)
    }

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        // Loop over all the touches in this event
        for touch: AnyObject in touches {
            // Get the location of the touch in this scene
            let location = touch.locationInNode(self)
            // Check if the location of the touch is within the button's bounds
            if button.containsPoint(location) {
                println("tapped!")
            }
        }
    }
}

If you need a button that looks and animates like the ones in UIKit, you'll need to implement that yourself; there's nothing built in to SpriteKit.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Create NavBar programmatically with Button and Title Swift

Swift add show action to button programmatically

How to take screen shot programmatically (Swift, SpriteKit)

Adding gestures to a button in swift

Moving a button programmatically not working - Swift

Adding and changing a custom UIView programmatically (Swift)

Adding buttons to toolbar programmatically in swift

Adding UIGestureRecognizer To Subview Programmatically in Swift

Segue and Button programmatically swift

Adding a button inside an array - Swift

Adding a text and image on a button programmatically in android

Button Constraints within a StackView (Swift Programmatically)

Issue with adding constraints to UIImage programmatically | Swift

button stackView spacing not working -Swift - Programmatically

Adding a view to a SpriteKit scene in Swift

Creating a button using SKLabelNode as a parent in Swift SpriteKit

adding popover to programmatically added UIBarButtonItem and adding images to the same popover Swift

Swift Adding Image to a Button

Setting my button's position programmatically in swift

Android adding button in FrameLayout programmatically

Swift + SpriteKit - Button is clickable even when not visible

iOS adding constraints programmatically in Swift

Adding a UIControl into title of Navbar Programmatically - Swift

Adding multiple instances of objects spritekit swift 3

Adding event to button programmatically

Adding a UIView to a UITableViewController with NSLayoutConstraints programmatically with Swift 4

Swift - Adding labels to stack views programmatically

Adding a badge to the round button - Swift

Favorite button programmatically Swift