Cocos2d v2.x(ios) - 如何进行循环滚动?

时间:2014-08-11 11:21:48

标签: ios cocos2d-iphone

我想用Cocos2d v2.x为IOS设备做一个小游戏。其中一个功能是圆形地图滚动 - 就像当您将屏幕滚动到地图的最右边时,它会跳回到最左侧。就像你旋转地球一样。 我的问题:在某些时候我必须渲染地图(所有游戏图形)两次 - 当你将屏幕移动到最右边缘时甚至更进一步的一半屏幕应该显示一块地图左端,一半应该显示地图的右端。如何以较少的痛苦实现它?

Map是CCNode的子类,包含许多子节点和一些自定义绘制方法。

我看到两种方式:  1.只需制作map-CCNode的副本并将其渲染到第一个附近:


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

  1. 制作CCRenderTexture,渲染地图并显示地图的一部分+ RenderTexture的一部分。
  2. 
       [----map----][RenderTex]
                 |-/ screen /-]
    
    

    我的两个想法都有缺点:

    • CCRenderTexture严重降低了性能,即使在iPad-4全屏大小的renderTexture上工作也只有20fps左右;

    • 地图root-CCNode的第二个副本将占用大量内存,特别是如果它有很多孩子;

    也许是否有一种方法可以使用应用的偏移量第二次渲染根映射CCNode?像:

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

    ? 感谢您的想法!

1 个答案:

答案 0 :(得分:0)

相当容易)

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

@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