~~NOTOC~~ ====== SXJoypad ====== ---- dataentry extension ---- type : extension #or 'mod' if it requires changing the Sparrow source author_mail : pixelrevision@gmail.com Malcolm description : A simple joypad for use with sparrow lastupdate_dt : 2011-04-11 #the date you created the extension compatible : v1.1 #the Sparrow version you tested the extension with depends : #if the ext. depends on others, list them here tags : Joypad, controller, movement #enter a few tags, separated by commas homepage_url : https://gist.github.com/914079 #if the ext. has an URL (e.g. a Gist-page), add it here download_url : https://gist.github.com/gists/914079/download #a direct link to the download (e.g. the Gist-archive) ---- ===== Usage ===== 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 #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 ===== Changelog ===== * //2011/04/11 21:01//: First public version ===== Source Code ===== %gist(914079)% ===== Discussion ===== Whoop, should have looked closed before adding. Also see Shilo's [[users:shilo:extensions:shthumbstick|shthumbstick]]