在Cocos2D中正确使用draw方法

时间:2012-10-15 17:45:09

标签: objective-c cocos2d-iphone draw

我想创建一个简单的图层来显示一些信息,比如分数。

我设法这样做,但我不确定它是否正确:

#import "InformationLayer.h"
#import "GameScene.h"

@implementation InformationLayer

-(void) draw
{
    // Complete clear of the screen
    [self removeAllChildrenWithCleanup:true];

    // Draw my score
    [self DrawScore];
}

- (void)DrawScore
{
    // create and initialize a label
    NSString* l_sScore = [NSString stringWithFormat:@"Score : %d", [[GameScene sharedGameScene] iScore]];

    CCLabelTTF* label = [CCLabelTTF labelWithString:l_sScore fontName:@"Marker Felt" fontSize:32];

    // get the window (screen) size from CCDirector
    CGSize size = [[CCDirector sharedDirector] winSize];

    // position the label at the center of the screen
    label.position = CGPointMake(size.width - ([label texture].contentSize.width / 2), size.height*2.0/3.0);

    // add the label as a child to this Layer
    [self addChild:label];
}
@end

我一直在处理我的场景和图层,而且我真的不能轻松清理所有图层,然后重新绘制所有内容。在我看来,这是一个完全矫枉过正!不过,它还能完成这项任务......

由于我处于学习曲线的最底层,我想快速做好...我能在这里做些更好的事吗?

2 个答案:

答案 0 :(得分:4)

这是完全错误的。仅添加一次节点。仅当您想使用OpenGL(或cocos2d的函数,如draw)绘制内容时,才使用ccDrawLine方法。您可以在一些init方法中添加内容(标签,精灵等)。

要更改标签文字,请使用setString:方法。

答案 1 :(得分:0)

遵循Morion的建议:

-(id) init
{
    if( (self=[super init] ))
    {
        [self InitLabels];

        // Scheduling the refresh method.
        [self scheduleUpdate]; 
    }
    return self;
}

// Update replaces draw !! 
-(void)update:(ccTime)delta
{
    [self UpdateLabels];
}

- (void)InitLabels
{
    // get the window (screen) size from CCDirector
    CGSize l_ScreenSize = [[CCDirector sharedDirector] winSize];
    float l_iScreenHeightPosition = l_ScreenSize.height*2.0/3.0;

    NSString* l_sScore = [NSString stringWithFormat:@"Score : %d", [[GameScene sharedGameScene] iScore]];
    NSString* l_sLevel = [NSString stringWithFormat:@"Level : %d", [[GameScene sharedGameScene] iLevel]];

    l_lblScore = [CCLabelTTF labelWithString:l_sScore
                                           fontName:@"Marker Felt" fontSize:32];

    l_lblScore.position = CGPointMake(l_ScreenSize.width - ([l_lblScore texture].contentSize.width / 2), l_iScreenHeightPosition);

    // add the label as a child to this Layer
    [self addChild:l_lblScore];

    l_lblLevel = [CCLabelTTF labelWithString:l_sLevel
                                                fontName:@"Marker Felt" fontSize:32];

    l_lblLevel.position = CGPointMake(l_ScreenSize.width - ([l_lblLevel texture].contentSize.width / 2), l_iScreenHeightPosition - ([l_lblScore texture].contentSize.height));

    // add the label as a child to this Layer
    [self addChild:l_lblLevel];
}

- (void)UpdateLabels
{
    NSString* l_sScore = [NSString stringWithFormat:@"Score : %d", [[GameScene sharedGameScene] iScore]];
    NSString* l_sLevel = [NSString stringWithFormat:@"Level : %d", [[GameScene sharedGameScene] iLevel]];

    [l_lblScore setString:l_sScore];
    [l_lblLevel setString:l_sLevel];
}

现在对我来说似乎更好:)