Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9106751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:24:44+00:00 2026-06-17T02:24:44+00:00

i have two layers control layer which contains CCLabelTTF for score, and first layer

  • 0

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…

my scene.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;


       }

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 !:(

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:24:45+00:00Added an answer on June 17, 2026 at 2:24 am

    Your statement cl=[controlLayer new]; is not getting the reference of the controlLayer that was added to your game scene. As your game scene.m code indicates, both FirstLayer and controlLayer instances are initalized and added as children to the CCScene. To get a proper reference to your controlLayer from inside FirstLayer, do it like this:

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

    The reason you have to put this in onEnter is because self.parent will not be valid within the FirstLayer’s init method. onEnter is called after initialization is complete. This should give you the reference to the actual controlLayer that was added to your game scene.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two layers. The first layer is Image control. The source of it
Please see: http://jsfiddle.net/XEbKy/3/ I have two layers of tabs. On the top layer I
I have markers in two marker layers,which i need to keep separate, so I
I am creating game in cocos 2D . I have two layers .layer one
Im creating a custom control which consist of two layers in a donut shape.
I have two layers being drawn. A static background texture and a texture(png) with
say I have two 'modules'. For instance the hardware-interface layer of a RS-232 port
I have two classes (MVC view model) which inherits from one abstract base class.
I have two update panels on a page. And I want to update both
I have two images and I want to multiply these two images together in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.