cocoa - In objective-c , How to make variable accessible from more than one class -


in cocos2d iphone have 2 classes:

1- gamescene.h class

2- player.h class

gamescene.h has label scorelabel2 declared follows

   @interface gamescene : cclayer {     player* player;     cclabel* scorelabel2; 

and player.h class has following method

 -(void)updatescore{       nslog(@"%@",scorelabel2);  } 

i getting error

          error: 'scorelabel2' undeclared (first use in function) 

what best solution problem since need use many objects , variables between classes?

many thanks

ahmed,

you need have updatescore method on scene , call method player class. recommend this:

@interface gamescene : cclayer { player* player; cclabel* scorelabel2; nsinteger score; ... } ... - (void) updatescorebyamount:(nsinteger)scoremodifier; 

andin .m have like:

- (void) updatescorebyamount:(nsinteger)scoremodifier {     score += scoremodifier;  // scoremodifier can positive or negative } 

then in player class call method on scene when want change score.

[myscene updatescorebyamount:5]; 

this need modified suit situation, looking for.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -