thanks geidsvig, i have done as you said and subsequently eliminating the possibility of miss typing the string!! which is great!!
Although... I would like to know if i could create a constant CGPoint? to remove the need of creating a Dictionary full of NSValues holding CGPoints. or if anyone can think of a better way around my current problem.
I am aware that i could simply use a CGPoint in my initialisation, removing the need for a Dictionary. which i may resort to!! but my logic (if it may be flawed) was to be able to initialise my object without having to check the co-ordinates of each sprite.
but the extra dictionary and NSValues seem like a bit overkill.. here is my code. let me know what you guys think..
ball.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "chipmunk.h"
extern NSString *const BLUE;
extern NSString *const YELLOW;
extern NSString *const RED;
extern NSString *const GREEN;
@interface Ball : CocosNode {
AtlasSpriteManager *mgr;
AtlasSprite *sprite;
cpBody *body;
cpShape *shape;
NSDictionary *locations;
@private
int d;
float r;
int mass;
}
@property (nonatomic,retain) AtlasSpriteManager *mgr;
@property (nonatomic,retain) AtlasSprite *sprite;
@property (nonatomic,retain) NSDictionary *locations;
-(void)spriteLocationSetup;
@end
ball.m
#import "Ball.h"
NSString *const BLUE = @"blue";
NSString *const YELLOW = @"yellow";
NSString *const RED = @"red";
NSString *const GREEN = @"green";
@implementation Ball
@synthesize mgr;
@synthesize sprite;
@synthesize locations;
-(id)initWithLocation:(CGPoint)p velocity:(CGPoint)v andColour:(NSString*)c {
self = [super init];
if (self) {
AtlasSpriteManager *m = [[AtlasSpriteManager alloc] initWithFile:@"balls.png" capacity:10];
self.mgr = m;
[m release];
[self spriteLocationSetup];
d = 26;
r = 13.25f;
mass = 1.0f;
CGPoint coords = [[locations objectForKey:c] CGPointValue];
AtlasSprite *s = [[AtlasSprite alloc] initWithRect:CGRectMake(coords.x, coords.y, d, d) spriteManager:mgr];
self.sprite = s;
[s release];
self.sprite.position = p;
body = cpBodyNew(mass, cpMomentForCircle(mass, 0.0, r, cpvzero));
body->p = p;
body->v = v;
shape = cpCircleShapeNew(body, r, cpvzero);
shape->e = 0.8; shape->u = 4;
shape->data = sprite;
}
return self;
}
-(void)spriteLocationSetup {
NSValue *yellow = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
NSValue *red = [NSValue valueWithCGPoint:CGPointMake(1, 0)];
NSValue *green = [NSValue valueWithCGPoint:CGPointMake(2, 0)];
NSValue *blue = [NSValue valueWithCGPoint:CGPointMake(3, 0)];
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:yellow,YELLOW,red,RED,green,GREEN,blue,BLUE,nil];
self.locations = dic;
[dic release];
}
-(void)dealloc {
[locations release];
[mgr release];
[sprite release];
[super dealloc];
}
@end
Byron