clops
clops

Reputation: 5235

Best Practice in Cocos2d

I am at the start of my cocos2d adventure, and have some ideological questions to ask. I am making a small space-shooter game, am I right to use the following class structure?

and a followup question — what is the best practice in accessing objects from other layers? For example, when the joystick is updated, it must rotate the space ship and move the background. Both of these are in other layers. Is there some recommended way to go about this or should I simply get the desired objects by Tag and operate on them?

Upvotes: 3

Views: 681

Answers (3)

codeperson
codeperson

Reputation: 8050

Regarding tags, one method I use to make sure the tag numbers I'm assigning are unique is define an enum as follows:

typedef enum
{
    kTag_BackgroundLayer = 100,
    kTag_BackgroundImage,
    kTag_GameLayer = 200,
    kTag_BadGuy,
    kTag_GoodGuy,
    kTag_Obstacle,
    kTag_ControlLayer = 300
    kTag_Joystick,
    kTag_Buttons
};

Most times I'll also just set zOrder and tag properties of CCNodes (i.e. CCSprites, CCLabelTTFs, etc.) the same, so you can actually use the enum to define your zOrder, too.

Upvotes: 1

johnbakers
johnbakers

Reputation: 24750

Cocos is a big singleton-based system, which may not appeal to some developers but is often used in Cocos apps and is the fundamental architecture of the framework. If you have one main scene and many subsequent layers added to that scene, and you want controls from one layer to affect sprites or logic on other layers, there really is nothing wrong with making your main scene a singleton and sending the information from the joystick layer back to the scene to handle for manipulating other layers or sprites. I do this all the the time and this technique is used in countless Cocos tutorials in books and online, so you can feel that you aren't breaking too many rules if you do it this way (and it's also quite easy to do).

If you instead choose to use pointers in one layer to send data to other layers, this can get you into a lot of trouble since one node should never own another node that it doesn't have a specific parent-child relationship with. Doing so can cause crashes and problems with the native Cocos cleanup methods when you remove scenes later, and potentially leak memory. You could use a weak reference in such a case instead, but that is still dependent on one layer expecting another layer to always be around, which may not be the case.

Sending data back to the main game scene to then dispatch and use accordingly is really efficient.

Upvotes: 3

James Webster
James Webster

Reputation: 32066

This seems like a perfectly reasonable way to arrange your objects, this is a method I use.

For accessing objects, I would keep an explicit reference to the object as a member variable and use it directly. (Using tags isn't a bad option, I just find it can get a little messy).

@interface Class1 : NSObject
{
    CCLayer *backgroundLayer;
    CCLayer *contentLayer;
    CCLayer *hudLayer;

    CCSprite *objectIMayNeedToUseOnBackgroundLayer;
    CCNode *objectIMayNeedToUseOnContentLayer;

}

Upvotes: 1

Related Questions