objective-c - Cocos2D计分游戏

标签 objective-c ios xcode sdk cocos2d-iphone

<分区>

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."

你能告诉我怎么做吗?请。链接到我的另一篇文章

----- Scoring System In Cocos2D

最佳答案

当您合成一个私有(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/

上一篇:ios - *** 由于未捕获的异常 'NSInvalidArgumentException' 终止应用程序,

下一篇:objective-c - 使用 AvFoundation 录制视频时,objective c 循环会阻止用户输入吗?

相关文章:

objective-c - Xcode Sqlite 编码土耳其字符

ios - RAC5 中缺少 rac_signalForControlEvents

iphone - 在 UIWebView 中同步加载 html 内容

objective-c - Cocoa:将参数传递给 NSApplicationDelegate

objective-c - NSMenuItem 未启用?

ios - 如何覆盖 Objective-C 中定义的值

iphone - 裁剪图像使图片向左旋转

objective-c - 何时释放CMBlockBufferRef?

ios - 尝试将供应商框架包装在 cocoapod 中,但编译器找不到 header

ios - 实现 CoverFlow View