Skip to content

Instantly share code, notes, and snippets.

@AsadR
Created July 22, 2011 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AsadR/1099876 to your computer and use it in GitHub Desktop.
Save AsadR/1099876 to your computer and use it in GitHub Desktop.
Cocos2D helper class to position sprites "by hand"
//
// SpritePositioning.h
//
//
// Created by Asad ur Rehman
//
#import "cocos2d.h"
@interface SpritePositioning : CCSprite <CCTargetedTouchDelegate, NSCoding> {
NSString *name;
NSDate *lastTap;
BOOL isDragging;
BOOL locked;
BOOL zwoptex;
CGPoint touchStartPoint;
}
+ (id)spriteWithFile:(NSString *)filename useZwoptex:(BOOL)z;
+ (id)spriteWithFile:(NSString *)filename;
- (id)initWithFile:(NSString *)filename useZwoptex:(BOOL)z;
- (id)initWithFile:(NSString *)filename;
- (void)lock;
- (void)unlock;
@end
//
// SpritePositioning.m
//
// Created by Asad ur Rehman
//
#import "SpritePositioning.h"
@implementation SpritePositioning
+ (id)spriteWithFile:(NSString *)filename useZwoptex:(BOOL)z {
return [[[self alloc] initWithFile:filename useZwoptex:z] autorelease];
}
+ (id)spriteWithFile:(NSString *)filename {
return [[[self alloc] initWithFile:filename useZwoptex:NO] autorelease];
}
- (id)initWithFile:(NSString *)filename useZwoptex:(BOOL)z {
if(z)
self = [super initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:filename]];
else
self = [super initWithFile:filename];
if(self != nil) {
name = [filename copy];
lastTap = nil;
zwoptex = z;
[self schedule:@selector(update:)];
}
return self;
}
- (id)initWithFile:(NSString *)filename {
return [self initWithFile:filename useZwoptex:NO];
}
- (id)initWithCoder:(NSCoder *)decoder {
name = [[decoder decodeObjectForKey:@"name"] retain];
zwoptex = [decoder decodeBoolForKey:@"zwoptex"];
if(zwoptex)
self = [super initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:name]];
else
self = [super initWithFile:name];
if( self != nil ) {
position_ = [decoder decodeCGPointForKey:@"position"];
lastTap = nil;
[self lock];
[self schedule:@selector(update:)];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeBool:zwoptex forKey:@"zwoptex"];
[coder encodeObject:name forKey:@"name"];
[coder encodeCGPoint:position_ forKey:@"position"];
}
- (void)dealloc {
[name release];
[lastTap release];
[super dealloc];
}
- (void)onEnter {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1000 swallowsTouches:YES];
[super onEnter];
}
- (void)onExit {
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint point = [self.parent convertTouchToNodeSpace:touch];
if(CGRectContainsPoint([self boundingBox], point)) {
if(lastTap && (-[lastTap timeIntervalSinceNow]) < 0.3) {
[self unlock];
[lastTap release];
lastTap = [[NSDate alloc] init];
//mach_absolute_time();
return YES;
}
if( isDragging == NO && locked == NO ) {
CCLOG(@"%@ is at (%.3f,%.3f)", name, point.x, point.y);
isDragging = YES;
touchStartPoint = point;
}
[lastTap release];
lastTap = [[NSDate alloc] init];
return YES;
}
else
return NO;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint point = [self.parent convertTouchToNodeSpace:touch];
if( isDragging ) {
[lastTap release];
lastTap = [[NSDate alloc] init];
self.position = ccpAdd(self.position, ccpSub(point, touchStartPoint));
touchStartPoint = point;
// CCLOG(@"%@ is now at (%.3f,%.3f)", name, point.x, point.y);
}
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
[self ccTouchMoved:touch withEvent:event];
if(isDragging)
isDragging = NO;
}
- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint point = [self.parent convertTouchToNodeSpace:touch];
CCLOG(@"%@ touches cancelled at (%.3f,%.3f)", name, point.x, point.y);
isDragging = NO;
}
- (void)update:(ccTime)dt {
float timeSinceLastMove = -[lastTap timeIntervalSinceNow];
if(timeSinceLastMove > 1.0 && isDragging) {
[self lock];
isDragging = NO;
}
}
- (void)lock {
locked = YES;
self.opacity = 255/2;
CCLOG(@"%@ locked at (%.3f,%.3f)", name, self.position.x, self.position.y);
}
- (void)unlock {
locked = NO;
self.opacity = 255;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment