2012-10-15 18 views
1

我想创建一个简单的图层来显示一些信息,如分数。正确使用Cocos2D中的绘制方法

我成功地做到这样,但是我真的不知道这是正确的:

#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 

我处理我的场景和层很像,所有的时间,我真的不舒适清除我的所有层然后再绘制一切。在我看来,这是一个彻头彻尾的矫枉过正!尽管如此,它的工作......

由于我在我的学习曲线的底部,我想快速做对......我可以在这里做更好的事情吗?

回答

4

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

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

+0

谢谢您的回答! –

+0

欢迎您=) – Morion

0

所以以下莫里恩的建议是:

-(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]; 
} 

看来,最好是我现在:)

+0

如果有提升空间,请随时发表评论! (我确定有:)) –

+0

无论如何,你不应该在绘制方法中做任何游戏逻辑。一种方法是通知你的代码改变显示值(我的意思是分数,时间等)。或者,如果您想每更改一次,请使用update:方法。你可以通过调用[self scheduleUpdate] – Morion

+0

更好吗?当然,我可以通知分数变化和等级变化,但这是我第一个应用程序,我试图逐步掌握一切......您的帮助非常宝贵,谢谢! –