2010년 10월 12일 화요일

|cocos2d best practice

Xcode Thumb
CCDirector 
-> DisplayLink (MainLoop, ThreadMainLoop)
Texture Atlas 
-> CCSprite (from CCSpriteSheet),
    CCBitmapFontAtlas or CCLabelAtlas (instead of CCLabel), 
    CCTMXTileMap or CCTileMapAtlas to render tiles
  => provide faster rendering 
Texture
-> 16-bit : png, gif, bmp, tiff images
        ex) [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixcelFormat_RGBB4444];
            4-bit, 2-bit : PVRTC
ex) CCSprite *sprite = [CCSprite spriteWithFile: @"sprite.pvr"];
Particles
-> faster at 1st and 2nd gen devides, but slower 3rd gen devices / iPad
Reducing Memory
-> use 16-bit or 4-bit textures
    use CCTextureCache (:caches all images, even not used image is remain in memory)
    To remove it from memory do:
ex) [[CCTextureCache sharedTextureCache] removeUnusedTextures]; // v0.8 ~
      ---------------------------------------------------------------------------------------
      CCTexture2D *texture = [sprite texture];
      [[CCTextureCache sharedTextureCache] removeTexture: texture]; // available in v0.7 too 
      ---------------------------------------------------------------------------------------
      [[CCTextureCache sharedTextureCache] removeAllTextures]; // available in v0.7 too
Timers
-> Not to use Cocoa's NSTimer instead use cocos2d's own scheduler
    cocos2d's scheduler : automatic pause/resume,
    : CCLayer(CCScene, CCSprite, CCNode) enters the stage the timer will be automatically activated and when it leaves the stage it will be automatically deactivated,
Your target/selector will be called with a delta time
Draw vs. update
-> draw selector : try not to update any state variable inside here -> pause/resume won't work as expected
  : draw everything inside here
    scheduled selector : try not to draw anything inside here -> it can't be transformed
      : update the state variable inside here
    draw is called every frame
    scheduled selectors can be called with any frame rate, but no more frequently than the applecation's FPS rate
Director flow control
-> If possible try to use replaceScene instead of pushScene
      pushScene is very handy, but it will put the pushed scene into memory, and memory is a precious resource in the iPhone
ex) [[CCDirector sharedDirector] pushScene: gameScene]; (X)
      [[CCDirector sharedDirector] replaceScene: gameScene]; (O)
Creating Nodes(CCSprite, CCLabel, etc….)
-> possible try to create CCNode objects (CCSprite, CCLabel, CCLayer, etc) in the init selector and not in any other scheduled selector,
           the creation of nodes expensive, so them pre-created. but if you don't use them frequently memory is wasted
Hierarchy of Layers
-> Don't create a big hierarchy of layers. Try to keep them low when possible
Actions
-> It is expensive to create certain actions since it might require a lot of malloc(). So try to reuse actions.
           Once the action is used, save it for later if you know you will execute that type of action again. 
    Then, instead of allocing a new action, you can just initialize it..
 Buttons
-> Use a Menu with a MenuItemImage and place your menu using meno.position = ccp(x,y). See the MenuTest example for more details
 How does the pause/resume works?
-> when the director receives the pause message it won't call any scheduled target/selector.
    but the draw selector will be called at a rate of 4 FPS (to reduce battery consumption)
    when the director receives the resume message, the scheduled target/selectors will be called again every frame.

댓글 없음:

댓글 쓰기