我接到了一个问题,我刚才问一个答复,但我是新来的编码,而且不知道该怎么做。下面是回复:
“ @synthesize一个 ”分数“ int类型的财产,而 ”scoreLabel 0 “ - ” 型CCLabelTTF财产
初始化你的分数财产。“ (无效)初始化
线126,增加你1 “得分” 的属性,并将该值设置成你的CCLabelTTF。“
你能告诉我如何做到这一点? PLZ。链接到我的其他职务
----- Scoring System In Cocos2D
我接到了一个问题,我刚才问一个答复,但我是新来的编码,而且不知道该怎么做。下面是回复:
“ @synthesize一个 ”分数“ int类型的财产,而 ”scoreLabel 0 “ - ” 型CCLabelTTF财产
初始化你的分数财产。“ (无效)初始化
线126,增加你1 “得分” 的属性,并将该值设置成你的CCLabelTTF。“
你能告诉我如何做到这一点? PLZ。链接到我的其他职务
----- Scoring System In Cocos2D
当你合成一个私有变量(其他类不能看到它),你让其他类的方式来查看和/或修改变量的值。
首先,你要创建的变量:
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
意味着无论如何应该显示为一个整数,而不是一个浮点数(有小数点)。
它看起来像你需要“实例化”你的标签,并将它作为一个孩子添加到图层。实例化只是创建某个事物的一个奇特术语。将“课堂”视为椅子的蓝图,将“实例”视为从该蓝图创建的椅子。一旦你创建了椅子(一个实例),你可以修改它(绘制它,添加/删除腿等)。
因此,实例化你的标签,并把它添加到层(本身):
-(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];
谢谢** SOOOOOOO很多!! ** – DarkMoonLLC 2012-08-16 00:18:26
没问题,我也忘了合成变量。但是,KK发布一个伟大的答案,说实话,他是更全面,更好的长期解决方案。 – ICanChange 2012-08-16 00:24:22
创建接口声明后score
财产HelloWorldLayer.h,像
@property (nonatomic, retain) int score;
然后合成它位于@implementation HelloWorldLayer
行后面的.m文件中。
创建设置和获取分数的方法:
-(int)getScore {
return self.score;
}
-(void)setScore:(int)newScore {
self.score = newScore;
}
在init
方法,属性的值设置为零,
if((self=[super init])) {
//... other stuff
[self setScore:0]
}
您可以更新setScore方法比分,但我建议有此另一种方法,调用setScore,这样就可以在不同的地方有一行调用中使用它,并像半一秒钟内等,在某些情况下,分配更多的得分,就像两个冲突的任何变化..
-(void)updateScore:(int)increment {
int currentScore = [self getScore];
[self setScore:(currentScore + increment)];
}
同样,对于标签,
@property (nonatomic, retain) CCLabelTTF scoreLabel; // in header
和
@synthesize scoreLabel; // in .m file
同样,在你的init方法,初始化label与位置,层和初始文本等等,那么你可以更新updateScore方法中的文本。
-(void)updateScore:(int)increment {
int currentScore = [self getScore];
[self setScore:(currentScore + increment)];
[scoreLabel setString:[NSString stringWithFormat:@"Score: %i", [self getScore]]];
}
确保您通过tutorial之前,为了避免关于常见任务的混乱提前去阅读。
您不应该发布这样的新问题,继续关于您的第一个问题的交谈 – 2012-08-16 00:06:02