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

Adding event to button programmatically

Adding a view to a SpriteKit scene in Swift

Segue and Button programmatically swift

Favorite button programmatically Swift

Android adding button in FrameLayout programmatically

How to take screen shot programmatically (Swift, SpriteKit)

Adding buttons to toolbar programmatically in swift

Adding UIGestureRecognizer To Subview Programmatically in Swift

iOS adding constraints programmatically in Swift

Adding gestures to a button in swift

Swift Adding Image to a Button

Adding multiple instances of objects spritekit swift 3

Moving a button programmatically not working - Swift

Swift + SpriteKit - Button is clickable even when not visible

Creating a button using SKLabelNode as a parent in Swift SpriteKit

Adding a text and image on a button programmatically in android

Swift - Adding labels to stack views programmatically

Adding and changing a custom UIView programmatically (Swift)

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

Adding a UIControl into title of Navbar Programmatically - Swift

Issue with adding constraints to UIImage programmatically | Swift

Adding a button inside an array - Swift

Adding a badge to the round button - Swift

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

Button Constraints within a StackView (Swift Programmatically)

Setting my button's position programmatically in swift

Create NavBar programmatically with Button and Title Swift

button stackView spacing not working -Swift - Programmatically

Swift add show action to button programmatically