Search code examples
ioscocos2d-iphonescalelayerscene

Cocos2d v2.0 - Set Up Scenes, Layers, Sprites, etc


With Cocos2d v2.0.0, a lot of changes were made and a lot of people are experiencing some scaling problems and other conflicts ...

This is even more true if they read and test out Ray Wenderlich's great book : Learning Cocos2d : The viking guy is to big, the background is not centered, the suffix system for images is not working, ...

So, how to proceed ?


Solution

  • Basically, everything is already told in the sample project when creating a new cocos2d v2 project. But, some of us need to get things pointed out.

    • Image Suffix System for iPhone (Non-retina & Retina) and iPad (Non-retina & Retina)

    Check out lines 68-71 from AppDelegate.m Here is what you need to remember :

    1. You just need to call

      CCSprite *mySprite = [CCSprite spriteWithFile:@"mySprite.png"];
      
    2. No need for some if(iPad) then else if(iPhone) blabla
    3. Make sure you called your images like as lines 68-71 from AppDelegate.m (Default : mySprite.png (iPhone) | mySprite-hd.png (iPhone Retina) | mySprite-ipad.png (iPad) | mySprite-ipadhd.png (iPad Retina)


    • Calling a Scene

    Check out line 76 from AppDelegate.m

    1. Just call the scene function from your class (using pushScene)

      [director_ pushScene: [MyScene scene]];
      


    • Adding layers to your scene class

    1. Supposing your scene class is called with the +(CCSene*)scene function (which is a good practice for cocos2d v2)
    2. +(CCScene *) scene
      {
          // 'scene' is an autorelease object.
          CCScene *scene = [CCScene node];
      
          BackgroundLayer *backgroundLayer = [BackgroundLayer node];
          [scene addChild:backgroundLayer z:0];
      
          GameplayLayer *gameplayLayer = [GameplayLayer node];
          [scene addChild:gameplayLayer z:5];
      
          return scene;
      }
      


    • Replacing the init function from your classes (CCScene, CCLayer, ...)

    This is where scaling problems from v2 of cocos2d come from (but I do not know why).

    1. Instead of calling -(id)init, call

      -(void) onEnter{ //Do some sprite displaying with [self addChild:mySprite]; }
      
    2. You can still call the -(id)init function for other things you need to load before displaying sprites.


    • Using SneakyInput (Joystick opensource library)

    You can find how to use sneakyinput with cocos2d v2 here : http://cl.ly/1J2D2z0f3o0r3h041o3o


    • Multi Touch Enabling

    1. Add this line to your layer (in the +(CCScene*) scene function or else where)

      layer.isTouchEnabled = YES;
      
    2. Then add this to the same .m

      - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
      {
          NSArray *touchArray=[touches allObjects];
      
          if ([touchArray count] == 2)
          {
              NSLog(@"2");
          }
          else if([touchArray count]==1)
          {
              NSLog(@"1");
          }
      }
      
    3. Finally, go to the AppDelegate.m and add this :

      [[CCDirector sharedDirector].view setMultipleTouchEnabled:YES];
      


    I would much appreciate if cocos2d v2 developers could help out and post stuff about how to use cocos2d v2 compared to v1.