SHThumbstick class will allow you to create multiple thumbsticks to a game with ease. You can choose to use images or go without images. You can change the thumbstick type to static, relative, absolute, float. More details about these types are in the sample project. There's many other properties to customize the thumbstick, but can also be as simple as you like.
//initialize a thumbstick SHThumbstick *absoluteThumbstick = [SHThumbstick thumbstick]; //set the inner image absoluteThumbstick.innerImage = [SPImage imageWithContentsOfFile:@"innerThumbstick.png"]; //set the outer image absoluteThumbstick.outerImage = [SPImage imageWithContentsOfFile:@"outerThumbstick.png"]; //change the thumbstick type to absolute, which will only appear on touch absoluteThumbstick.type = SHThumbstickAbsolute; //set the bounds for landscape absoluteThumbstick.bounds = [SPRectangle rectangleWithX:0 y:0 width:480 height:320]; //change the inner radius to 0 absoluteThumbstick.innerRadius = 0; //change the outer radius to 40 absoluteThumbstick.outerRadius = 40; //draw the values of innerRadius, outerRadius, and bounds to screen absoluteThumbstick.debugDraw = YES; //add an event listener to get the event.distance and event.direction [self addEventListener:@selector(onThumstickChanged:) atObject:self forType:SH_THUMBSTICK_EVENT_CHANGED]; //add the thumbstick to the stage [self addChild:absoluteThumbstick];
- (id)initWithWidth:(float)width height:(float)height { if (self = [super initWithWidth:width height:height]) { SHThumbstick *thumbstick = [SHThumbstick thumbstick]; thumbstick.innerImage = [SPImage imageWithContentsOfFile:@"innerThumbstick.png"]; thumbstick.outerImage = [SPImage imageWithContentsOfFile:@"outerThumbstick.png"]; thumbstick.debugDraw = YES; [self addChild:thumbstick]; [thumbstick addEventListener:@selector(onThumbstickTouch:) atObject:self forType:SH_THUMBSTICK_EVENT_TOUCH]; [thumbstick addEventListener:@selector(onThumbstickMove:) atObject:self forType:SH_THUMBSTICK_EVENT_MOVE]; [thumbstick addEventListener:@selector(onThumbstickTouchUp:) atObject:self forType:SH_THUMBSTICK_EVENT_TOUCHUP]; [thumbstick addEventListener:@selector(onThumbstickChanged:) atObject:self forType:SH_THUMBSTICK_EVENT_CHANGED]; } return self; } - (void)onThumbstickTouch:(SHThumbstickEvent *)event { NSLog(@"onThumbstickTouch distance:%f direction:%f", event.distance, event.direction); } - (void)onThumbstickMove:(SHThumbstickEvent *)event { NSLog(@"onThumbstickMove distance:%f direction:%f", event.distance, event.direction); } - (void)onThumbstickTouchUp:(SHThumbstickEvent *)event { NSLog(@"onThumbstickTouchUp distance:%f direction:%f", event.distance, event.direction); } - (void)onThumbstickChanged:(SHThumbstickEvent *)event { NSLog(@"onThumbstickChanged distance:%f direction:%f", event.distance, event.direction); }
#include "SHThumbstick.h"
#import "SHThumbstick.h"