1

I am creating a game where I created a UI for my main menu screen and I want to apply the play button to show on the play button shown in the image below. I know how to do it via with main.storyboard but don't know how in spritekit. So, when they click on the play button it will go to a different scene which is the (GameScene)

here is the image.

enter image description here

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Jonathon
  • 39
  • 9

2 Answers2

2

You can approach that in two ways. Either you stick with SpriteKit and handle interaction yourself or you mix SpriteKit with UIKit. In the first case, you would subclass SKSpriteNode or create a SKNode subclass with a SKSpriteNode child

SKSpriteNode(imageNamed: "...")

and handle user interaction by overriding the touchesBegan or touchesEnded functions. Or you go with UIButton created with code.

UPDATE

Decided to make a quick generic implementation of a SpriteKit button. You can try it here, but keep in mind it was a quick first commit:)

konrad.bajtyngier
  • 1,786
  • 12
  • 13
  • Is it really safe to mix and match `SpriteKit` and `UIKit` frameworks? Would be nice… ;-) – Paulo Mattos Apr 21 '17 at 12:07
  • It kind of depends on your project. It sometimes causes issues to add `UIKit` elements because they appear on top of `SpriteKit`. But works fine in some cases. – konrad.bajtyngier Apr 21 '17 at 13:12
  • In the case of a button/gui controls, it's not a big issue to have them on top. It's actually what you should want. – BadgerBadger Apr 21 '17 at 15:44
  • @konrad.bajtyngier Nice custom object, I have one very similar that I use as well. Have you considered using a lazy implementation of your TitleLabel? It would clean up your initialization of that object a bit. – Ron Myschuk Apr 21 '17 at 17:48
  • @RonMyschuk good idea, I'll consider in a next commit, thanks! – konrad.bajtyngier Apr 21 '17 at 20:49
1

I wouldn't have a single image for your scene that has the buttons included on it. There is no room for scalability like that. I would create those buttons as a subclass of SKSpriteNode and add them to the scene as objects. That way if you move around the button you don't have to redo your background image. @Konrad.bajtyngier Is on the right path with his implementation, and I use something very similar in all of my games (it becomes an indispensable object that gets copied from game to game). His implementation uses closures () -> () to carry out the actions, there is another option in you can use in your sub class as well.

in your subclass add a delegate

protocol ButtonDelegate: NSObjectProtocol {
    func buttonWasPressed(sender: Key)
}

weak var buttonDelegate: ButtonDelegate!

and when you create your button just add they scene as the delegate of the button.

button.buttonDelegate = self

and then in the scene add the buttonWasPressed func. This way you'll will know what button was pressed and handle it accordingly and not have to worry about your objects getting retained by using closures.

I don't feel one method is better than the other, but you should strongly consider sub classing your button, and remove the button backgrounds from your background image.

please see my answer and code here for full details

Make touch-area for SKLabelNode bigger for small characters

Community
  • 1
  • 1
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32