【问题标题】:touching a sprite in cocos2D crashs my app在 cocos2D 中触摸精灵会使我的应用程序崩溃
【发布时间】:2012-02-28 21:17:16
【问题描述】:

我的代码有问题:

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

【问题讨论】:

  • 您能提供一些有关崩溃的信息吗?调试器说什么?
  • 我的应用停止了,这就是我所知道的全部
  • 设置断点并单步执行并检查值以发现问题所在。

标签: iphone xcode crash cocos2d-iphone touch


【解决方案1】:

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,并在删除oeuf1ombreOeuf1 时将其设置为YES/true。那么如果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;
        }
    }
}

【讨论】:

  • 不运行是什么意思,请问我怎样才能用代码做到这一点。我是法国人,所以我可以更好地理解代码
  • Pas de problème - j'espère que tous va bien。 (如果法语错了,请见谅!)
  • 您对此有回应吗:stackoverflow.com/questions/9505792/…
【解决方案2】:

这样做

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

如果您想知道问题的原因,请查看@下面的答案

【讨论】:

    猜你喜欢
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多