Swift Spritekit Adding Button programmatically

后端 未结 5 682
独厮守ぢ
独厮守ぢ 2020-12-01 11:46

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 r

5条回答
  •  悲哀的现实
    2020-12-01 11:53

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

    SgButton(normalImageNamed: "back.png")
    

    Create button with textures:

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

    Create round corner text button:

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

提交回复
热议问题