This is a simple joypad implementation for sparrow to be able to just pop on the stage and move things around. You can use directional input, the angle from the center, or a projection that is based on the width of the image. If your image is not centered, you can offset centerPoint to make input closer to what you want.
#import <Foundation/Foundation.h> #import "SXJoypad.h" @interface SparrowJoypad : SPStage{ SXJoypad *joypad; SPQuad *quad; } @end @implementation SparrowJoypad - (id)initWithWidth:(float)width height:(float)height { self = [super initWithWidth:width height:height]; joypad = [SXJoypad joypadWithContentsOfFile:@"joypad.png"]; [self addChild:joypad]; quad = [SPQuad quadWithWidth:16 height:16 color:0xFF0000]; [self addChild:quad]; // [self addEventListener:@selector(updateJoypadWithDirection:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME]; [self addEventListener:@selector(updateJoypadWithAngle:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME]; return self; } - (void)updateJoypadWithDirection:(SPEnterFrameEvent*)event{ // state version float speed = 1; if(joypad.verticalDirection == kSXJoypadVerticleDirectionTop){ quad.y -= speed; }else if(joypad.verticalDirection == kSXJoypadVerticleDirectionBottom){ quad.y += speed; } if(joypad.horizontalDirection == kSXJoypadHorizontalDirectionLeft){ quad.x -= speed; }else if(joypad.horizontalDirection == kSXJoypadHorizontalDirectionRight){ quad.x += speed; } } - (void)updateJoypadWithAngle:(SPEnterFrameEvent*)event{ // somewhat analog float speed = 1; if(joypad.recievingInput){ quad.x += cos(joypad.angle) * speed; quad.y += sin(joypad.angle) * speed; } } @end
Whoop, should have looked closed before adding. Also see Shilo's shthumbstick