0

我已经为 cocos2D 中的 CCLayer 实现了触摸结束。当收到触摸时,它会调用一个方法。但是,调用该方法似乎总是导致应用程序由于“EXC_BAD_ACCESS”而崩溃在我调用的方法中,我尝试从 NSMutableDictionary 中读取这是我的代码:

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:[touch view]];
    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
    int current = 1;
    int i = 1;
    for (i=1; i<=4; i++) {
        CCNode *sprite = [layer2 getChildByTag:i];
        CGPoint worldCoord = [layer2 convertToWorldSpace:sprite.position];
        CGRect bounds = CGRectMake(worldCoord.x-sprite.boundingBox.size.width, worldCoord.y-sprite.boundingBox.size.height/2, sprite.boundingBox.size.width, sprite.boundingBox.size.height);
        //CCLOG(@"Sprite%i:%f,%f at %f,%f",i,bounds.size.width,bounds.size.height,bounds.origin.x,bounds.origin.y);
        if (CGRectContainsPoint(bounds, touchPoint)) {
            CCLOG(@"touched sprite:%i",i);
            current = i;
            [self checkSpriteTouch:current]; //error occurs when this method is called

            break;
        }
    }
}
-(void)registerWithTouchDispatcher{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:NO];
}


-(void)checkSpriteTouch:(int)i{
    NSMutableDictionary *dict = [storeDict objectForKey:[NSString stringWithFormat:@"Char%i",1]]; //when using debugging, app crashes here at this line
    NSNumber *boughtValue = [dict objectForKey:@"Bought"];
}

为什么我的应用程序崩溃?我在其他 touchEnded 方法中实现的所有其他方法都可以正常工作......提前谢谢您!非常感谢任何帮助^_^

4

3 回答 3

0

storeDict 是空的还是零?

我也认为应该是 i 而不是 1

NSMutableDictionary *dict = [storeDict objectForKey:[NSString stringWithFormat:@"Char%i",i]];
于 2012-04-25T04:06:43.617 回答
0

所以(int)i什么也没做checkSpriteTouch,我不知道为什么你必须把 i 放在你的方法中。

此外,您应该检查storeDict崩溃时“i”值的实现。也许您必须在此处输入代码,以便我们检查。

于 2012-04-28T17:18:01.920 回答
0

我终于弄清楚了为什么访问该方法会导致我的应用程序崩溃。答案是访问字典。我没有正确保留它,从而导致错误的访问警告。为了解决这个问题,我在头文件中使用了:@property(nonatomic,retain)NSMutableDictionary *storeDict; 现在,一切正常!^_^

于 2012-04-30T19:57:51.437 回答