2013-02-05 81 views
1

我想通过触摸来滚动我的地图。你能告诉我如何达到它的基本概念吗?这是我丑陋的简单代码。只需通过每次触摸移动即可修改图层的位置。它的工作原理,但它可能并不可爱。如何顺利滚动CCLayer?

.h 
CGPoint *touchBegin; 
CGPoint *touchmove; 

.m 


    -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 


    CGPoint touchLocation = [touch locationInView:[touch view]]; 
    touchLocation = [[CCDirector sharedDirector] 
        convertToGL:touchLocation]; 
    touchBegin=new CGPoint(touchLocation); 
} 



    -(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { 
     CGPoint touchLocation = [touch locationInView:[touch view]]; 
     touchLocation = 
     [[CCDirector sharedDirector] convertToGL:touchLocation]; 
     touchmove = new CGPoint(touchLocation); 
     [self setPosition:ccp(self.position.x/100-(touchBegin->x-touchmove->x),self.position.y/100.0f)]; 
     delete touchmove; 
      touchmove=NULL; 
} 


-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { 
     if (touchBegin) 
     { 
      delete touchBegin; 
      touchBegin=NULL; 
     } 

     if (touchmove) 
     { 
      delete touchmove; 
      touchmove=NULL; 
     } 
    } 
+0

可爱的意思是什么?请记住,将图层移动到触摸位置时,如果不在触摸之间进行插值(触摸位置可能差异很大)并逐渐向最后一个触摸位置移动(通过使用CCMoveTo更新每帧的位置或作为拐杖),将永远不会获得平滑的结果。 – LearnCocos2D

+0

@ LearnCocos2D,谢谢,听起来很有趣。我想尝试自己实现这一点。练习。 –

回答

0

尝试CCScrollLayer扩展。 Here is link

«首先这两个文件添加到项目中

«在场景中导入CCScrollLayer.h

«在场景中的init()方法构造的每一层,并将它传递给CCScrollLayer类

对于更多细节请参考CCScrollLayerTestLayer.m中的cocos2d-iphone-extensions

+0

谢谢。但是如果我有一个大图层并想通过滚动触摸进行导航? –

+0

CCScrollLayerTestLayer:在模拟器中播放此示例.. – Guru