2012-02-28 56 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); 

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