触摸结束不会调用方法

时间:2012-04-24 19:57:45

标签: objective-c ios cocos2d-iphone nsdictionary

我已经为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方法中实现的所有其他方法都可以完美地运行... 先感谢您!任何帮助都非常感谢^ _ ^

3 个答案:

答案 0 :(得分:0)

storeDict是空的还是零?

此外,我认为应该是我而不是1

NSMutableDictionary *dict = [storeDict objectForKey:[NSString stringWithFormat:@"Char%i",i]];

答案 1 :(得分:0)

所以(int)icheckSpriteTouch中没有做任何事情,我不知道你为什么要把我放在你的方法中。

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

答案 2 :(得分:0)

我终于弄明白为什么访问该方法会导致我的应用程序崩溃。 答案是访问词典。 我没有正确保留它,因此导致访问错误警告。 要修复它,在头文件中,我使用:     @property(nonatomic,retain)NSMutableDictionary * storeDict; 现在,一切正常! ^ _ ^