2013-02-05 84 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