<分区>
<分区>
Possible Duplicate:
Scoring System In Cocos2D
我之前提出的问题得到了回复,但我是编码新手,不知道如何操作。回复如下:
"@synthesize a "score" property of type int, and a "scoreLabel" property of type CCLabelTTF.
initialize your score property to "0" in -(void)init
On line 126, increment your "score" property by 1, and set that value into your CCLabelTTF."
你能告诉我怎么做吗?请。链接到我的另一篇文章
最佳答案
当您合成一个私有(private)变量(其他类看不到它)时,您允许其他类看到和/或修改该变量的值。
首先,您要创建变量:
NSMutableArray *_targets;
NSMutableArray *_projectiles;
int _score;
CCLabelTTF *_scoreLabel;
然后在你的 init 方法中将 _score
设置为 0
:
-(id) init
{
if( (self=[super init] )) {
[self schedule:@selector(update:)];
_score = 0;
然后递增(加 1)您的 _score
变量并将您的 _scoreLabel
的字符串(文本内容)设置为该值。
if (CGRectIntersectsRect(projectileRect, targetRect)) {
[targetsToDelete addObject:target];
_score++;
[_scoreLabel setString:[NSString stringWithFormat:@"%d", _score]];
}
行 [_scoreLabel setString:[NSString stringWithFormat:@"%d", _score]];
是一种将 _score
的整数转换为字符串 ( NSString
)。这是一种古老的 C 实现方式,%d
表示无论将要出现的内容都应显示为整数,而不是 float (具有小数点)。
看起来您还需要“实例化”您的标签并将其作为子项添加到图层中。实例化只是创建某物实例的一个奇特术语。将“类”想象成椅子的蓝图,将“实例”想象成从该蓝图创建的椅子。创建椅子(实例)后,您可以对其进行修改(对其进行绘画、添加/删除腿等)。
因此,要实例化您的标签并将其添加到层(本身):
-(id) init
{
if( (self=[super init] )) {
[self schedule:@selector(update:)];
_score = 0;
//Create label
_scoreLabel = [CCLabelTTF labelWithString:@"0" fontName:@"Marker Felt" fontSize:16];
//Add it to a layer (itself)
[self addChild:_scoreLabel];
关于objective-c - Cocos2D计分游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11978588/