I've got an array of 12 AtlasSprites, any of which can be touched by the player.
In my init function, I store all of the sprites in an NSArray.
In my touchesBegan function, I enumerate through the array to see if any touches are on one of my sprites. My problem is that the array doesn't seem to exist by the time it gets there - The simulator quits every time I try to acces the NSArray from the touchesBegan function. Any ideas?
Here's the code:
myNode.h :
@interface hackNode : Layer {
AtlasSprite *keySprite1;
.....
AtlasSprite *keySprite12;
//// This is the array that's giving me problems
NSArray *keyArray;
////
}
+(Scene*) scene;
-(void) initKeys;
@end
myNode.m :
@implementation myNode
+(Scene*) scene {
Scene *s = [Scene node];
id game = [myNode node];
[s addChild:game];
return s;
}
-(id) init
{
[super init];
isTouchEnabled = YES;
[self initKeys];
return self;
}
-(void) initKeys {
//puts all our keys in an array, then initializes them
AtlasSpriteManager *keyManager = [[AtlasSpriteManager spriteManagerWithFile:@"KeySpriteSheet.png" capacity:5] retain];
keySprite1 = [[AtlasSprite spriteWithRect:CGRectMake(0,0,60,60) spriteManager:keyManager]retain];
keySprite12 = [[AtlasSprite spriteWithRect:CGRectMake( 0,0,60,60) spriteManager:keyManager]retain];
//// keyArray is filled here /////
keyArray = [NSArray arrayWithObjects: keySprite1, keySprite2, keySprite3, keySprite4, keySprite5, keySprite6, keySprite7, keySprite8, keySprite9, keySprite10, keySprite11, keySprite12, nil];
//////
[self addChild:keyManager z:-1];
}
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches) {
CGPoint location = [touch locationInView: [touch view]];
NSLog(@"test1");
///This next line causes the program to fail
NSLog(@"%@",keyArray);
//////
}
}