Access variable from 1 layer to another

Access variable from 1 layer to another
0.0 0


I want to access a variable from 1 layer to another.
I tried implementing few things but all failed.

I used CC_SYNTHESIZE(int, _mNumber, mNumber);
It gave bad access in other layer when I called

@ int score = ((Samplecocos2dx *)this~~>getParent~~>getChildByTag)>getMNumber;
2. I used a method to return the score. It also gave bad access
3. I set tag for the layer itself.
this->getParent()->addChild(GameOverLayer::node(), 2, mNumber);
When I tried to get
int score = this->getTag();
Its returning

Can anyone give me a solution???


Could you paste more codes or attach one demo project? :slight_smile:


I just want to access 1 variable from 1 layer to another… Whatever be the way. Please tell me. It surely doesn’t need code for ma whole game… :slight_smile: :stuck_out_tongue:


Hi Nikhil,
I use a class for all “global” objects, methods, etc.
In this post you will find and “example” (ejem…) well doesn’t pretend to be an example, but you could find an explanation there:

‘File.h’ and ‘File.cpp’ should act as this global class, accessed by anyother through ‘File::foo’ after include it.

Another way to manage this - speaking about simple variables - is using CCFileUtils:

Hope it helps,
Jordi :slight_smile: