Access scene with singleton or via CCDirector? And question about layer interaction
I've read a book that is when I have multiple layers in scene I should set the layer as property of scene and get them by using scene's singleton to get that scene then call it's property.
But I thought I can use [[CCDirector sharedDirector]runningScene]
and I can get the scene so I can access the property without using singleton. Is this the better way?
Also I've read Cocos2D concept on it's website told me that Scene contains layer, which gives the scene a behavior and such. This means I should set up my 开发者_Go百科layers so that they can talk to each other and do their thing without messing with scene? (other than changing scene which I should call CCDirector) But I think if I use my scene to command my layers that would be a better way? Because scene have all references of layers it would be easier to program than to have layer talking with each other...
Thanks!
Your question is rather confusing. A scene can have many layers. My current game in development has one scene (during the main game) with several layers that perform in various ways. If you need another class to be able to control a layer then there are various ways you can do this.
- Pass the pointer to a method of the outside class
- Have a property to store the pointer and set it from the class that created the layer
- Have the layer in it's own CCLayer subclass and make it a singleton
- Have a small singleton class that stores pointers to all layers that will need to be shared around.
- Or whatever else you think up
if you want to access a layer in running scene from another class (witch means also another layer in same scene) you can use [[CCDirector sharedDirector]runningScene] to get a reference to scene and then getChildByTag: to get a reference to the specific layer.
when you want to access a layer in same scene it is better to do these calls in onEnter method of your layer not init method.
精彩评论