好吧,我的代码有问题:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView: [myTouch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
CGRect MoveableSpriteRect =CGRectMake(oeuf1.position.x -(oeuf1.contentSize.width/2),oeuf1.position.y-(oeuf1.contentSize.height/2),oeuf1.contentSize.width,oeuf1.contentSize.height);
if (CGRectContainsPoint(MoveableSpriteRect, location)) {
[self removeChild:oeuf1 cleanup: YES];
[self removeChild:ombreOeuf1 cleanup: YES];
}
}
当我触摸 oeuf1 时,它消失了,但是我再次触摸屏幕时,我的应用崩溃了,我不知道为什么?请问该如何解决?谢谢 。对不起,我的英语我是法语:/
最佳答案
一旦删除,CGRect MoveableSpriteRect =CGRectMake(oeuf1.position.x -(oeuf1.contentSize.width/2),oeuf1.position.y-(oeuf1.contentSize.height/2),oeuf1.contentSize.width,oeuf1.contentSize.height);
行仍然引用oeuf1
,因此将导致EXC_BAD_ACCESS
错误。解决该问题的最简单方法是在头文件中声明BOOL
,并在删除YES
和true
时将其设置为oeuf1
/ ombreOeuf1
。然后,如果BOOL
为true,请不要运行
CGRect MoveableSpriteRect =CGRectMake(oeuf1.position.x -(oeuf1.contentSize.width/2),oeuf1.position.y-(oeuf1.contentSize.height/2),oeuf1.contentSize.width,oeuf1.contentSize.height);
if (CGRectContainsPoint(MoveableSpriteRect, location)) {
[self removeChild:oeuf1 cleanup: YES];
[self removeChild:ombreOeuf1 cleanup: YES];
}
编辑:
在您的
.h
文件中添加:@interface .... {
...
BOOL oeuf1Removed; // Feel free to translate this to French!
}
然后将
-touchesBegan
更改为:- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if (!oeuf1Removed) {
CGRect MoveableSpriteRect = CGRectMake(oeuf1.position.x -(oeuf1.contentSize.width/2),oeuf1.position.y-(oeuf1.contentSize.height/2),oeuf1.contentSize.width,oeuf1.contentSize.height);
if (CGRectContainsPoint(MoveableSpriteRect, location)) {
[self removeChild:oeuf1 cleanup: YES];
[self removeChild:ombreOeuf1 cleanup: YES];
oeuf1Removed = YES;
}
}
}