касаясь спрайта в cocos2D, вылетает мое приложение - PullRequest
0 голосов
/ 29 февраля 2012

хорошо, у меня есть проблема с моим кодом:

- (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 , он исчезает, как я хотел, но затем, если я снова касаюсь экрана, мое приложение перестает работать, я нене знаю почему?как я могу решить это, пожалуйста?благодарю вас .Извините за мой английский, я французский: /

Ответы [ 2 ]

2 голосов
/ 29 февраля 2012

Строка 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 истинно, не запускайте

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];
}

EDIT:

В вашем .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 голосов
/ 29 февраля 2012

сделать это

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);

если вы хотите узнать причину проблемы, посмотрите @ ответ ниже

...