Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i have two layers control layer which contains CCLabelTTF for score, and first layer in which i detect collision btwn my objects. i want to update score (i.e CCLabelTTF value) in control layer from my first layer.

here is my code...

control layer.h

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

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

-(void)displayScore;    
@end

controlLayer.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];        
}

firstLayer.h

@interface FirstLayer : CCLayer     
{           
    controlLayer *cl;                
    }

@property(nonatomic,retain)controlLayer *cl;    

@end

firstLayer.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];                    

            }               

        }                 
    }

What is going Wrong? , i'm always getting score:test 0 !:(

share|improve this question
Where do you think the problem is? How have you tried to solve it? What does your code do? – Anko Jan 5 at 14:46
I don't feel like going through your code line by line but at a glance it doesn't look like it's being updated anyway. On the layer [self scheduleUpdate] then in your update method, have the score = whateverScoreIs [nameOfClass updateLabelMethod:score] or something to that effect. – Tony Jan 7 at 19:11

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.