简体   繁体   English

触摸cocos2D中的精灵会使我的应用程序崩溃

[英]touching a sprite in cocos2D crashs my app

well I have a problem with my code : 好吧,我的代码有问题:

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

When I touch oeuf1 it disappear like I wanted but then if i touch the screen again my app crash I don't know why ? 当我触摸oeuf1它就像我想要的那样消失但是如果我再次触摸屏幕我的应用程序崩溃我不知道为什么? how can I solve this please ? 我该怎么解决这个问题? thank you . 谢谢 。 Sorry for my english I'm french :/ 对不起我的英语我是法国人:/

The line 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); is still referencing oeuf1 once it has been removed, and so will cause an EXC_BAD_ACCESS error. 删除后仍然引用oeuf1 ,因此会导致EXC_BAD_ACCESS错误。 The simplest way to fix it is to declare a BOOL in your header file, and set it to YES / true when you remove oeuf1 and ombreOeuf1 . 解决它的最简单方法是在头文件中声明一个BOOL ,并在删除oeuf1ombreOeuf1时将其设置为YES / true Then if the BOOL is true, don't run 然后,如果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];
}

EDIT: 编辑:

In your .h file add: 在你的.h文件中添加:

@interface .... {
    ...
    BOOL oeuf1Removed; // Feel free to translate this to French!
}

And then change -touchesBegan to: 然后改变-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;
        }
    }
}

do this 做这个

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

instead of just 而不仅仅是

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

if you want the cause of the problem look @ the below answer 如果你想问题的原因看起来@下面的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM