Game Development Reference
In-Depth Information
joystick = [[SneakyJoystick alloc] initWithRect: ←
CGRectMake(0, 0, stickRadius, stickRadius)];
joystick.autoCenter = YES;
joystick.hasDeadzone = YES;
joystick.deadRadius = 10;
CCSprite* back = [CCSprite spriteWithSpriteFrameName:@"joystick-back.png"];
CCSprite* thumb = [CCSprite spriteWithSpriteFrameName:@"joystick-stick.png"];
SneakyJoystickSkinnedBase* skinStick = [[SneakyJoystickSkinnedBase alloc] init];
skinStick.joystick = joystick;
skinStick.backgroundSprite.color = ccYELLOW;
skinStick.backgroundSprite = back;
skinStick.thumbSprite = thumb;
skinStick.position = CGPointMake(stickRadius * 1.5f, stickRadius * 1.5f);
[self addChild:skinStick];
The SneakyJoystick is initialized with a CGRect , and contrary to the Sneak-
yButton , the CGRect is actually used to determine the joystick's radius. I set the
joystick to autoCenter so that the thumb controller jumps back to the neutral posi-
tion, like most real-world game controllers. The dead zone is also enabled; this is a
small area defined by the deadRadius in which you can move the thumb controller
without any effect. This gives users a certain radius where they can keep the thumb
controller centered. Without the dead zone, it would be almost impossible to center the
thumb controller manually.
The SneakyJoystickSkinnedBase is positioned a small distance away from the
edge of the screen. The button's position and size may not be ideal for the game, but it
demonstrates the controls better. If you align the thumb controller with the screen
edges, it's too easy to inadvertently move your finger off the touchscreen and thus lose
control of the ship.
Before you can test the joystick you also have to send the addJoystick message to
the InputLayer class. Just add this next to the addFireButton in the init
-(id) init
Search Nedrilad ::

Custom Search