SXJoypad

type:
extension
author:
Malcolm
description:
A simple joypad for use with sparrow
lastupdate:
2011-04-11
compatible:
v1.1
tag:
Joypad, controller, movement
homepage:
https://gist.github.com/914079
download:
https://gist.github.com/gists/914079/download

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 <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

Changelog

  • 2011/04/11 21:01: First public version

Source Code

Discussion

Whoop, should have looked closed before adding. Also see Shilo's shthumbstick

  extensions/sx_joypad.txt · Last modified: 2015/09/14 11:15 (external edit)
 
Powered by DokuWiki