У меня готова игра, и сейчас я пытаюсь изменить код.Я получил класс Spider из CCNode и использовал метод целевого делегата CCTargetedTouchDelegate.
@interface Spider : CCNode<CCTargetedTouchDelegate> {
CCSprite* spiderSprite;
NSString * spiderKilled;
int killed;
AppDelegate *del;
}
+(id) spiderWithParentNode:(CCNode*)parentNode;
-(id) initWithParentNode:(CCNode*)parentNode;
@end
При касании паук должен быть убит, и вот код:
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint tch = [touch locationInView: [touch view]];
CGPoint touchLocation = [[CCDirector sharedDirector] convertToGL:tch];
// Check if this touch is on the Spider's sprite.
BOOL isTouchHandled = CGRectContainsPoint([spiderSprite boundingBox], touchLocation);
if (isTouchHandled)
{
j = j + 1;
killed ++;
[del setKilledScore:j];
[self removeChild:spiderSprite cleanup:YES];
}
return isTouchHandled;
}
Я добавляю 10 пауков в слое GameScene, используя: -
for(int i=0; i <10 ;i++){
[Spider spiderWithParentNode:self];
}
Но, к сожалению, я не могу удалить пауков и выдает ошибку EXC_BAD_ACCESS в этой строке: [self removeChild:spiderSprite cleanup:YES];
Пожалуйста, помогите мне преодолеть эту ошибку.
Спасибо
Обновление - код инициализации Spider // Статический инициализатор автоматического выпуска, имитирующий схему распределения памяти в cocos2d.+ (id) spiderWithParentNode: (CCNode *) parentNode {return [[[self alloc] initWithParentNode: parentNode] autorelease];}
-(id) initWithParentNode:(CCNode*)parentNode
{
if ((self = [super init]))
{
[parentNode addChild:self];
del = [[UIApplication sharedApplication] delegate];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
spiderSprite = [CCSprite spriteWithFile:@"spider.png"];
spiderSprite.position = CGPointMake(CCRANDOM_0_1() * screenSize.width, CCRANDOM_0_1() * screenSize.height);
[self addChild:spiderSprite];
// Manually add this class as receiver of targeted touch events.
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];
}
return self;
}