Swift Spritekit Adding Button Software

How to programmatically add a button that will trigger an action when pressed? What code will be used?

I used to just add a button to the storyboard and run IBAction from there.

+8
swift
source share
5 answers

Adding a button to SpriteKit and responding to clicks on it is not as easy as in UIKit. You basically need to create SKNode kind of SKNode that draws your button, and then checks to see if the touches registered in your scene will be within the node.

In fact, a simple scene with a single red rectangle in the center acting like 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 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 bounds if button.containsPoint(location) { println("tapped!") } } } } 

If you need a button that looks and comes to life, as in UIKit, you need to implement this yourself; nothing is built into SpriteKit there.

+21
source share

I created the SgButton class ( https://github.com/nguyenpham/sgbutton ) in Swift to create buttons for SpriteKit. You can create buttons with images, textures (from SpriteSheet), text and background images / textures. For example, to create a button with an image:

 SgButton(normalImageNamed: "back.png") 

Create a button with textures:

 SgButton(normalTexture: buttonSheet.buy(), highlightedTexture: buttonSheet.buy_d(), buttonFunc: tappedButton) 

Create a round text button:

 SgButton(normalString: "Tap me", normalStringColor: UIColor.blueColor(), size: CGSizeMake(200, 40), cornerRadius: 10.0, buttonFunc: tappedButton) 
+13
source share

Mike S - Answer updated for - Swift 3.0

 override func didMove(to view: SKView) { createButton() } func createButton() { // Create a simple red rectangle that 100x44 button = SKSpriteNode(color: SKColor.red, size: CGSize(width: 100, height: 44)) // Put it in the center of the scene button.position = CGPoint(x:self.frame.midX, y:self.frame.midY); self.addChild(button) } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { let touch = touches.first let touchLocation = touch!.location(in: self) // Check if the location of the touch is within the button bounds if button.contains(touchLocation) { print("tapped!") } } 
+1
source share

You can use OOButtonNode . Text / Image Buttons, Swift 4.

0
source share
  func tappedButton(theButton: UIButton!) { println("button tapped") } } 

The above code displays the button that was pressed when the button was pressed.

PS fast ebook - a really good guide for a new programming language.

-5
source share

All Articles