I have touches ended implemented for a CCLayer in cocos2D.
When a touch is received, it calls a method.. however, calling the method seems to always result in the app crashing due to “EXC_BAD_ACCESS”
In the method that I call, I try to read from a NSMutableDictionary
Here’s my code:
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
int current = 1;
int i = 1;
for (i=1; i<=4; i++) {
CCNode *sprite = [layer2 getChildByTag:i];
CGPoint worldCoord = [layer2 convertToWorldSpace:sprite.position];
CGRect bounds = CGRectMake(worldCoord.x-sprite.boundingBox.size.width, worldCoord.y-sprite.boundingBox.size.height/2, sprite.boundingBox.size.width, sprite.boundingBox.size.height);
//CCLOG(@"Sprite%i:%f,%f at %f,%f",i,bounds.size.width,bounds.size.height,bounds.origin.x,bounds.origin.y);
if (CGRectContainsPoint(bounds, touchPoint)) {
CCLOG(@"touched sprite:%i",i);
current = i;
[self checkSpriteTouch:current]; //error occurs when this method is called
break;
}
}
}
-(void)registerWithTouchDispatcher{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:NO];
}
-(void)checkSpriteTouch:(int)i{
NSMutableDictionary *dict = [storeDict objectForKey:[NSString stringWithFormat:@"Char%i",1]]; //when using debugging, app crashes here at this line
NSNumber *boughtValue = [dict objectForKey:@"Bought"];
}
Why does my application crash? All other methods which I implement in my other touchEnded methods work perfectly fine…
Thank you in advance! Any help is GREATLY appreciated ^_^
I finally figured out why accessing that method would cause my app to crash.
The answer was in accessing the Dictionary.
I had not retained it properly, thus causing the bad access warnings.
To fix it, in the header file, I used:
@property(nonatomic,retain)NSMutableDictionary *storeDict;
and now, everything works fine! ^_^