Objective c 触摸结束后将不会调用方法
我已经在cocos2D中实现了一个CCLayer。 当收到触摸时,它调用一个方法。。然而,调用该方法似乎总是会导致应用程序由于EXC\u BAD\u访问而崩溃 在我调用的方法中,我尝试从NSMutableDictionary读取数据 这是我的密码:Objective c 触摸结束后将不会调用方法,objective-c,ios,cocos2d-iphone,nsdictionary,Objective C,Ios,Cocos2d Iphone,Nsdictionary,我已经在cocos2D中实现了一个CCLayer。 当收到触摸时,它调用一个方法。。然而,调用该方法似乎总是会导致应用程序由于EXC\u BAD\u访问而崩溃 在我调用的方法中,我尝试从NSMutableDictionary读取数据 这是我的密码: - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchPoint = [touch locationInView:[touch view
- (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方法中实现的所有其他方法都非常好。。。
提前谢谢你!非常感谢您的帮助^ ^storeDict是空的还是空的 另外,我认为应该是i而不是1
NSMutableDictionary *dict = [storeDict objectForKey:[NSString stringWithFormat:@"Char%i",i]];
所以inti在checkSpriteTouch中什么也没做,我不知道你为什么要在你的方法中加入I
此外,您应该检查storeDict的实现,它崩溃时i的值。也许你必须把代码放在这里,这样我们才能检查它。我终于明白了为什么访问该方法会导致我的应用程序崩溃。 答案是查阅字典。 我没有正确地保留它,因此导致错误的访问警告。 为了修复它,在头文件中,我使用了: @PropertyNationomic,保留可编辑字典*存储目录;
现在,一切都很好^_^ 如果storeDict为空或为零,则不会崩溃。storeDict一定是垃圾,也许它在创建时从未被保留过?嗯,我不认为这是因为这两个原因。我的存储目录肯定不是空的,因为我记录了它并得到了肯定的结果。是因为我试图从我的商店目录中获取对象吗?您是如何填充/创建商店目录的?投票关闭,因为太本地化了;你的问题中没有包含足够的信息,因此无法得出作为答案发布的结论。