I read up on this error and it's supposedly thrown when I access a variable or object that has been released. But I'm not sure how to fix this error! I'm using Cocos2D to program a basic SpriteController that keeps track of all my Sprites and sends them a touchBegan: to keep track of touches.
I made a Basic HelloWorldScene.m that initializes like this:
And then, when the Scene is touched, I pass it on to the SpriteController
Finally, the SpriteController loops through all its Sprites, checks if they respond to touchBegan: and sends them the command.
The program displays the 2 red1.gif boxes on the screen during initialization fine. And after debugging around, I figured that the flow of the program does enter the TouchBegan and is passed to the SpriteController. However, as soon as I begin looping through allMySprites, I get the EXC_BAD_ACCESS error. After playing around with it, I determined that I simply can't access any of the objects in allMySprites beyond initialization. So even if I added a [allMySprites lastObject] directly in the scene, it'd throw the error. Why can't I access the objects in allMySprites? They don't seem to be released by the time I want to use them, but it just doesn't let me touch them.
EDIT: Removed all the smiley's in my code
I made a Basic HelloWorldScene.m that initializes like this:
-(id) init
{
if( (self=[super init] )) {
[SpriteController addSprite:[TouchBox touchBoxWithFile: @"red1.gif"]];
[[[SpriteController allMySprites] lastObject] setPosition: ccp(360, 180)];
[self addChild:[[SpriteController allMySprites] lastObject]];
[SpriteController addSprite:[TouchBox touchBoxWithFile: @"red1.gif"]];
[[[SpriteController allMySprites] lastObject] setPosition: ccp(120, 180)];
[self addChild:[[SpriteController allMySprites] lastObject]];
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self
priority:0
swallowsTouches:YES];
}
return self;
}
And then, when the Scene is touched, I pass it on to the SpriteController
- (BOOL) ccTouchBegan: (UITouch *)touch withEvent: (UIEvent *)event
{
CGPoint location = [[CCDirector sharedDirector] convertToGL: [touch locationInView: [touch view]]];
BOOL anyoneTouched = [SpriteController sendTouchBegan:location];
return anyoneTouched;
}
Finally, the SpriteController loops through all its Sprites, checks if they respond to touchBegan: and sends them the command.
+(BOOL) sendTouchBegan: (CGPoint) cgp
{
BOOL touched = FALSE;
if (allMySprites == nil) {
//touched = false;
} else {
for (CCSprite *sprite in allMySprites) {
if([sprite respondsToSelectorselector(touchBegan: )]) {
touched = [sprite touchBegan: cgp];
}
}
}
return touched;
}
The program displays the 2 red1.gif boxes on the screen during initialization fine. And after debugging around, I figured that the flow of the program does enter the TouchBegan and is passed to the SpriteController. However, as soon as I begin looping through allMySprites, I get the EXC_BAD_ACCESS error. After playing around with it, I determined that I simply can't access any of the objects in allMySprites beyond initialization. So even if I added a [allMySprites lastObject] directly in the scene, it'd throw the error. Why can't I access the objects in allMySprites? They don't seem to be released by the time I want to use them, but it just doesn't let me touch them.
EDIT: Removed all the smiley's in my code