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.
I've made my own Button-Class that I'm working with. SKButton.h:
SKButton.m:
An example: To initialize a button, you write the following lines:
Furthermore you need the 'buttonAction' method in your class. * No warranty that this class is working right in every case. I'm still quite new to objective-c. *
I had created a class for using SKSpriteNode as a button quite a while ago. You can find it on GitHub here.
AGSpriteButton
It's implementation is based on UIButton, so if you are already familiar with iOS, you should find it easy to work with.
It can also be assigned a block or an SKAction to be executed when the button is pressed.
It includes a method to set up a label as well.
A button will typically be declared like so:
And that's it. You're good to go.
you could use a SKSpriteNode as your button, and then when the user touches, check if that was the node touched. Use the SKSpriteNode's name property to identify the node:
Add node to your scene:
Handle touches:
What a lot of great solutions to this problem! For the hardcore scrollers that make it down this far, you're in for a treat! I have subclassed
SKScene
, and it takes ONE function call to register ANY node to act like aUIButton
! Here is the class:}
It includes a lot of ideas I haven't yet implemented and some explanations of the code, but just copy and paste it into your project, and you can use it as-is in your own scene. Here is a complete example usage:
The one caveat, is if you implement
touchesBegan
,touchesMoved
,touchesEnded
, and/ortouchesCancelled
you MUST CALL SUPER! Or else it will not work.And please realize that in that example, there is really only ONE LINE OF CODE you need to give ANY NODE
UIButton
characteristics! It was this line:I'm always open for ideas and suggestions. Leave 'em in the comments and Happy Coding!!
Oops, I forgot to mention I use this nifty extension. You can take it out of an extension (as you probably don't need it in every node) and plop it in my class. I only use it in one place.
}
And since all of us aren't targeting iOS, here's the start of some code I wrote to handle mouse interaction on the Mac.
Question for the gurus: does MacOS offer touch events when using a trackpad? Or are these sent into SpriteKit as mouse events?
Another question for the gurus, shouldn't this class properly be called SKButtonNode?
Anyway, try this...
I have used SKButton class by Graf.
I use the SKButton to do scene navigation. i.e present another scene when the user press the SKButton. I get
EXC_BAD_ACCESS
error attouchesEnded->[self setIsSelected:NO]
. This happens especially frequently on the latest iPad with fast CPU.After checking and troubleshooting, I realised that the SKButton object is already "deallocated" when the
setIsSelected
function is being called. This is because I use the SKButton to navigate to next scene and this also means that the current scene can be deallocated any time.I made a small change by putting the setIsSelected in the "else" portion as follows.
Hope this helps for other developer who also see the same error.