I'm discovering that UIButtons
don't work very well with SKScene
, So I'm attempting to subclass SKNode
to make a button in SpriteKit
.
The way I would like it to work is that if I initialize a button in SKScene
and enable touch events, then the button will call a method in my SKScene
when it is pressed.
I'd appreciate any advice that would lead me to finding the solution to this problem. Thanks.
For people writing their games in Swift! I have rewritten the essential parts of Graf's solution to a swift class. Hope it helps:
I wasn't convinced of any of the above options, so based on the latest Swift4 I created my own solution.
Here's another version based on Filip's Swift code. I've just simplified it a little and allowed it to take blocks rather than only selectors :
Use it like this :
Hope this helps.
Unfortunately SpriteKit does not have button node, I do not know why, because it is very useful control. So I decided to create my own and share via CocoaPods, please use it OOButtonNode. Buttons can use text/background or images, written in Swift 4.
Edit: I've made a github repo for my SKButtonNode that I'll hopefully be keeping current and updating as swift evolves!
SKButtonNode
Unfortunately I cannot comment yet on Filip's swift implementation of SKButton in Swift. Super happy that he made this in Swift! But, I noticed that he didn't include a function to add text to the button. This is a huge feature to me, so that you don't have to create separate assets for every single button, rather just the background and add dynamic text.
I added a simple function to add a text label to SKButton. It likely isn't perfect--I'm new to Swift just like everyone else! Feel free to comment and help me update this to the best it can be. Hope you guys like!
Sample creation of button:
Full Class Listed Below:
}