Reputation:
Here is my code for my "Share" button:
ShareButton = UIButton(frame: CGRect(x: 0, y:0, width: view.frame.size.width / 3, height: 60))
ShareButton.center = CGPointMake(CGRectGetMidX(self.frame), 3*CGRectGetHeight(self.frame)/4)
ShareButton.setTitle("Share", forState: UIControlState.Normal)
ShareButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
ShareButton.addTarget(self, action: ("pressed:"), forControlEvents: .TouchUpInside)
self.view?.addSubview(ShareButton)
PS: My Share button works but when the user shares his score and wants to play again, the Share button shows up on the Game Scene.
Upvotes: 1
Views: 285
Reputation: 10674
The first rule when making a SpriteKit game is to try to not use UIKit. All of your UI should be created directly in the SKScenes using SpriteKit APIs (SKLabelNodes, SKSpriteNodes, SKNodes etc). There are exceptions to this, like maybe using UICollectionViews for massive level select menus, but basic UI should never be done using UIKit.
So you should be making your buttons using SKSpriteNodes and add them directly to the SKScene you want.
There are plenty of tutorials to google on how to do this, a simple one is this
https://nathandemick.com/2014/09/buttons-sprite-kit-using-swift/
For a more complete one check out apples sample game "DemoBots" or have a look at these cool projects on gitHub.
https://github.com/nguyenpham/sgbutton
https://github.com/jozemite/JKButtonNode
In SpriteKit games you only tend to have 1 view Controller (GameViewController) which will present all your SKScenes (GameScene, MenuScene etc). If you use UIKit elements they get added to the GameViewController, and therefore they will be shown in all scenes (like your share button).
self.view?.addSubview(shareButton) // self.view is your GameViewController
If you have a game with more than 1 SKScene and quite a few buttons this will be madness to manage.
On the other hand if you use SpriteKit APIs, and because every SKScene starts in a clean state when you enter it, you dont have to worry about any of this.
If you insist on using UIKit than you will have to either remove or hide the share button before you transition to game scene and unhide it or add it again when you want to.
shareButton.isHidden = true
or
shareButton.removeFromSuperview()
Finally as good practice your properties should start with small letters not capital letters
shareButton = ...
Hope this helps
Upvotes: 3
Reputation: 1740
As stated, you should try to not use UIKit as that's for UIKit applications and not necessarily games.
You can use my JKButtonNode class to create buttons. The class file is right here. And the best part is that they are fully compatible with SpriteKit because they are made of an SKTexture and an SKLabelNode.
First, create the button at the top level of your class.
var shareButton: JKButtonNode?
Then in your didMoveToView or wherever you configured your button. (Except the init method as calling a function of the class itself before being initialized will not work) enter the following.
let shareButtonBackground = SKShapeNode(rect: CGRect(x: 0, y: 0, width: view.frame.size.width / 3, height: 60))
shareButton = JKButtonNode(title: "Share", background: SKView().textureFromNode(shareButtonBackground)!, action: shareButtonAction)
shareButton?.title.fontColor = UIColor.whiteColor()
shareButton?.canChangeState = false
shareButton?.canPlaySounds = false
addChild(shareButton!)
And you will get an error since you didn't declare the button's action but then just add it anywhere in your class.
func shareButtonAction(button: JKButtonNode) {
print("The share button has been pressed.")
}
These buttons work just like UIButtons. They can even change states if you want them to. And of course when you don't want it displayed, then just remove it from the parent. You can also make it more customizable; just look in the first link I gave you. Example screenshot. You can change the title properties too just by calling setTitleProperties.
Upvotes: 2