SKSimpleButton is a simple button implementation for SpriteKit
.
Manually add to your project:
- Add
SKSimpleButton.h
, andSKSimpleButton.m
to your project. - Add button action sounds.
Import SKSimpleButton.h
first.
#import "SKSimpleButton.h"
Load two textures and initialize button with textures.
/* Add a button with textures */
SKTextureAtlas * atlas = [SKTextureAtlas atlasNamed:@"button"];
SKTexture * defaultTexture = [atlas textureNamed:@"button-home-off"];
SKTexture * touchedTexture = [atlas textureNamed:@"button-home-on"];
SKSimpleButton * homeButton = [[SKSimpleButton alloc] initWithDefaultTexture:defaultTexture
andTouchedTexture:touchedTexture];
/* set position and method */
homeButton.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
The button works with a block.
[homeButton setMethod:^{
myLabel.text = @"BOOM!";
myLabel.fontSize = myLabel.fontSize + 10.0;
}];
/* add button as a node */
[self addChild:homeButton];
Easy as that. Actually, I wrote sample code for SpriteKit
so if you want to see, check the repository.
SKSimpleButton is really simple implementation. It's for beginners such as myself. If you are professional, please improve this button or make new wonderful button.
I learned SpriteKit
from SpriteKit Tutorial for Beginners. Thanks for writing the articles at all.
Also thanks to Weird Meetup Members for helping me to learn Objective-C.