Xcode 如何在cocos2d中增加分数标签
所以我有一个标签,我希望当一个图像与另一个图像碰撞时,它增加10。分数=分数+10;但我不知道为什么,它反而增加了40。这是我的代码:Xcode 如何在cocos2d中增加分数标签,xcode,cocos2d-iphone,label,increment,Xcode,Cocos2d Iphone,Label,Increment,所以我有一个标签,我希望当一个图像与另一个图像碰撞时,它增加10。分数=分数+10;但我不知道为什么,它反而增加了40。这是我的代码: -(id) init { if( (self=[super init] )) { [self schedule:@selector(update:)]; score = 0; scoreLabel = [CCLabelTTF labelWithString:[NSString stringWithFo
-(id) init
{
if( (self=[super init] )) {
[self schedule:@selector(update:)];
score = 0;
scoreLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d",score] fontName:@"PUSAB___.TTF" fontSize:15 ];
scoreLabel.position=ccp(450,30);
[self addChild:scoreLabel];
}
}
- (void)update:(ccTime)dt {
if (CGRectIntersectsRect(mangeurRect, targetRect)) {
[targetsToDelete addObject:target];
score=score + 10; ;// Not really, but your score changes somehow...
[scoreLabel setString: [NSString stringWithFormat:@"%d",score]];
}
}
对不起,我的英语是法语:/您可能应该检查您的目标是否已经在要删除的目标中,如下所示,以防实际清空targetsToDelete结构的过程以某种方式延迟了几次:
- (void)update:(ccTime)dt {
if ([targetsToDelete containsObject:target]) return; // already scored.
if (CGRectIntersectsRect(mangeurRect, targetRect)) {
[targetsToDelete addObject:target];
score=score + 10; ;// Not really, but your score changes somehow...
[scoreLabel setString: [NSString stringWithFormat:@"%d",score]];
}
}
上面假设targetsToDelete是一个NSMutableArray。我认为在CGRectIntersectsRect中没有得到任何帧。您应该尝试下面的代码
- (void)update:(ccTime)dt {
if (CGRectIntersectsRect([mangeurRect frame], [targetRect frame])) {
[targetsToDelete addObject:target];
score += 10;
[scoreLabel setString: [NSString stringWithFormat:@"%d",score]];
}
}
或
- (void)update:(ccTime)dt {
if (CGRectIntersectsRect([mangeurRect boundingBox], [targetRect boundingBox])) {
[targetsToDelete addObject:target];
score += 10;
[scoreLabel setString: [NSString stringWithFormat:@"%d",score]];
}
}
这样,每当图像相互碰撞时,分数可能会增加 您的更新方法不完整。试试这个:
- (void)update:(ccTime)dt {
if (CGRectIntersectsRect(mangeurRect, targetRect)) {
[targetsToDelete addObject:target];
score=score + 10; ;// Not really, but your score changes somehow...
[scoreLabel setString: [NSString stringWithFormat:@"%d",score]];
//do something with your targetsToDelete array..
for (CCSprite *target in targetsToDelete) {
//[_targets removeObject:target]; //uncomment this line, if you have saved your targets in a _targets array
[self removeChild:target cleanup:YES];
}
}
}
参考:你的英语很好。但是,代码的格式设置…:)英雄联盟那么问题出在哪里呢??)如何初始化/获取“mangeurRect”和“targetRect”?这些屏幕相对矩形是否需要更新每帧或恒定的局部精灵边界框?这些是在更新方法中初始化的。是否良好?尝试向代码中添加一些日志,并根据正在检查的对象查看分数何时更改。