2012-02-28 60 views
0

好,我有我的代码有问题:触摸在cocos2d精灵crashs我的应用程序

- (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它像消失,我想,但这时如果我再次触摸屏幕我的应用程序崩溃我不知道为什么?我该如何解决这个问题?谢谢 。对不起,我的英语我是法国人:/

+0

你能给有关崩溃的一些信息的问题的原因是什么?调试器说什么? – Dancreek 2012-02-28 21:35:51

+0

我的应用程序停机,相当这是我所知道 – 2012-02-28 21:36:17

+0

设置断点,单步调试和检验值,发现哪里出了问题。 – 2012-02-28 22:51:36

回答

2

线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当您删除oeuf1ombreOeuf1。这时如果BOOL是真实的,不跑

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; 
     } 
    } 
} 
+0

你是什么意思,不跑我该如何用代码做这件事。我是法国人,所以我可以更好地理解代码 – 2012-02-29 19:57:01

+0

看看我的编辑。 – jrtc27 2012-02-29 20:33:35

+0

加莱problème - j'espère阙TOUS VA边。 (对不起,如果法国人是错的!) – jrtc27 2012-02-29 23:01:14

0

为此

if (oeuf1) { 
CGRect MoveableSpriteRect = CGRectMake(oeuf1.position.x -(oeuf1.contentSize.width/2), 
             oeuf1.position.y-(oeuf1.contentSize.height/2), 
             oeuf1.contentSize.width,oeuf1.contentSize.height); 
} 

,而不是仅仅

CGRect MoveableSpriteRect = CGRectMake(oeuf1.position.x -(oeuf1.contentSize.width/2), 
             oeuf1.position.y-(oeuf1.contentSize.height/2), 
             oeuf1.contentSize.width,oeuf1.contentSize.height); 

,如果你想看看@下面的答案