By building on the last recipe, we can create a more sophisticated virtual analog stick. This input method measures vector magnitude as well as direction. We've also created a cool looking visual effect for the analog stick.
Execute the following code:
#import "TouchableSprite.h" //AnalogStick.h @interface AnalogStick : TouchableSprite { @public CGPoint _pressedVector; //Internal _pressedVector with no outer dead zone CCSprite *nub; CCSprite *bar; int direction; } @property (readonly) CGPoint pressedVector; //External pressedVector with a dead zone @end @implementation AnalogStick -(id)init { self = [super init]; if (self != nil) { self.scale = 0.5f; _pressedVector = ccp(0,0); CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache]; [cache addSpriteFramesWithFile:@"analog_stick.plist"]; ...