Search code examples
ioscocos2d-iphone

Cocos2d v2.x (ios) - how to do circular scrolling?


I want to do a little game for IOS-devices with Cocos2d v2.x. One of it's features is circular map scrolling - it's like when you scroll the screen to the most right edge of map, it jumps back to the left-most side. Like you rotate the globe. My problem: in some point of time i has to render map (all the game graphics) twice - when you move screen to the most right edge and even bit further one half of screen should display a piece of map left-end, and half should display right-end of map. How to achieve it with less pain?

Map is sub-class of CCNode with lots of childs and some custom draw methods.

I see two ways: 1. Just make a copy of map-CCNode and render it near first one:


     [---map---][---map-copy---]
           |-/screen/-|

  1. Make CCRenderTexture, render map to it and display part of map + part of RenderTexture.

   [----map----][RenderTex]
             |-/ screen /-]

Both of my ideas has disadvantages:

  • CCRenderTexture horribly slows performance, even on iPad-4 full-screen-size renderTexture works at about 20fps only;

  • second copy of map root-CCNode will eat much of memory, especially if it has lots of children;

Maybe is there a way of rendering root-map CCNode second time with applied offset? Like:


     [self offsetMap:deltaX];
     [self.map vizit];

? Thank for ideas!


Solution

  • quite an easy )

    class worldMap : CCNode;

    @interface mainScene : CCNode
    {
    -(id)init;
    }
    @property worldMap theMap;
    @end
    
    @implementation
    -(id)init
    {
    ... usual blah-blah here
     [self addChild:theMap];
    }
    
    -(void)visit
    {
      [super visit];
    
      CGPoint oldMapPos = self.theMap.position;
      CGPoint newMapPos = ccp(oldMapPos.x + self.theMap.width, oldMapPos.y);
      self.theMap.position = newMapPos;
      [self.theMap visit];
      self.theMap.position = oldMapPos;
    }
    @end