0

我有两层控制层,其中包含用于得分的 CCLabelTTF,第一层用于检测我的对象的碰撞。我想从我的第一层更新控制层中的分数(即 CCLabelTTF 值)。

这是我的代码...

我的场景.m

-(id)init{

    self=[super init];   

    FirstLayer *fl=[FirstLayer node];

    [self addChild:fl];

    controlLayer *cl=[controlLayer node];

    [self addChild:cl z:3 tag:1];

    return self;


       }

控制层.h

@interface controlLayer : CCLayer{
     CCLabelTTF * score ;
     int score_value;        
}

@property(nonatomic,retain)CCLabelTTF * score ;
@property int score_value;

-(void)displayScore;    
@end

控制层.m

-(id)init{        
   // my code..            
        [self displayScore];            
    }

    return self;
}


-(void)displayScore{        
    CGSize screenSize=[[CCDirector sharedDirector]winSize];    
    CCLabelTTF * score_lbl = [CCLabelTTF labelWithString:@"Score" fontName:@"Arial" fontSize:16.0];    
    score_lbl.position=ccp(screenSize.width*0.10,screenSize.height*0.90);        
    [self addChild:score_lbl z:99];

    score =[CCLabelTTF labelWithString:[NSString stringWithFormat:@"test:%d",score_value] fontName:@"Arial" fontSize:16.0] ;          
    NSString *str = [score string];        
    NSLog(@"SCORE control:%@",str);    
    score.position=ccp(screenSize.width*0.20,screenSize.height*0.90);        
    [self addChild:score];        
}

第一层.h

@interface FirstLayer : CCLayer     
{           
    controlLayer *cl;                
    }

@property(nonatomic,retain)controlLayer *cl;    

@end

第一层.m

@implementation FirstLayer

@synthesize cl;    
    -(id)init{

 ---

cl=[controlLayer new];
[self schedule:@selector(tick:)];

    return self;

}


-(void)tick:(ccTime)dt{           
    bool blockFound=false;        
    world->Step(dt, 10, 10);      

    std::vector<MyContact>::iterator pos;
    for(pos = _contactListener->_contacts.begin(); 
        pos != _contactListener->_contacts.end(); ++pos) {
        MyContact contact = *pos;          

        b2Body *bodyA = contact.fixtureA->GetBody();
        b2Body *bodyB = contact.fixtureB->GetBody();
        if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
            CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
            CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();                         

            // Sprite A = ball, Sprite B = Block
            if (spriteA.tag == 1 && spriteB.tag == 2) {                    

                cl.score_value=cl.score_value+5;

                [cl.score setString:[NSString stringWithFormat:@"%d",cl.score_value]];                                   

                NSString *str = [cl.score string];

                NSLog(@"SCORE in GAME:%@",str);

               // [cl displayScore];                        

                if (std::find(toDestroy.begin(), toDestroy.end(), bodyB) 
                    == toDestroy.end()) {
                    toDestroy.push_back(bodyB);

                }
            }
            // Sprite B = block, Sprite A = ball
            else if (spriteA.tag == 2 && spriteB.tag == 1) {                      

                cl.score_value=cl.score_value+5;

               [cl.score setString:[NSString stringWithFormat:@"%d",cl.score_value]];

                NSString *str = [cl.score string];

                NSLog(@"SCORE in GAME:%@",str);

             //    [cl displayScore];                    

            }               

        }                 
    }

出了什么问题?,我总是得到分数:测试 0 !:(

4

2 回答 2

1

您的声明cl=[controlLayer new];没有获得添加到游戏场景中的 controlLayer 的引用。正如您的游戏 scene.m 代码所示,FirstLayer 和 controlLayer 实例都已初始化并作为子项添加到 CCScene。要从 FirstLayer 中正确引用您的 controlLayer,请执行以下操作:

FirstLayer.m

-(void) onEnter {
[super onEnter];
cl = (controlLayer*)[self.parent getChildByTag:1];
}

您必须将其放入 onEnter 的原因self.parent是在 FirstLayer 的 init 方法中无效。 onEnter初始化完成后调用。这应该为您提供对添加到游戏场景中的实际 controlLayer 的引用。

于 2013-01-05T13:38:44.483 回答
-1

我认为从外观上看,您正在向控制层发送一个新的分数值,但您并没有更新标签,因为您只设置了一次。如果 score_value 发生变化,它不会自动更新。我认为您需要在控制层中安排一个更新方法来更新分数标签。

于 2013-01-05T12:52:24.480 回答