2011-08-28 81 views
0

當我在我的iphone上運行我的應用程序(不在模擬器上)時,只有當我開始移動地圖時纔會出現奇怪的黑線。因此,這裏是我的移動tilemap的代碼:iphone遊戲編程:tilemap黑線問題

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer { 

    if (recognizer.state == UIGestureRecognizerStateBegan) {  

     CGPoint touchLocation = [recognizer locationInView:recognizer.view]; 
     touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 
     touchLocation = [self convertToNodeSpace:touchLocation];     

    } else if (recognizer.state == UIGestureRecognizerStateChanged) {  

     CGPoint translation = [recognizer translationInView:recognizer.view]; 
     translation = ccp(translation.x, -translation.y); 
     CGPoint newPos = ccpAdd(self.position, translation); 
     self.position = [self boundLayerPos:newPos]; 
     [recognizer setTranslation:CGPointZero inView:recognizer.view];  

    } else if (recognizer.state == UIGestureRecognizerStateEnded) { 

     float scrollDuration = 0.2; 
     CGPoint velocity = [recognizer velocityInView:recognizer.view]; 
     CGPoint newPos = ccpAdd(self.position, ccpMult(ccp(velocity.x, velocity.y * -1), scrollDuration)); 
     newPos = [self boundLayerPos:newPos]; 

     [self stopAllActions]; 
     CCMoveTo *moveTo = [CCMoveTo actionWithDuration:scrollDuration position:newPos];    
     [self runAction:[CCEaseOut actionWithAction:moveTo rate:1]];    

    }  
} 

回答

2

累積增量會產生由於浮點四捨五入問題假象。將瓦片放置在空間的固定位置,並通過仿射變換移動所有內容,可以獲得更好的效果。一箇中間解決方案是累積一個絕對偏移量並將其添加到每個tile的起始位置(您顯然必須將每個起始位置緩存在某處)。

+0

嗯,我是一種noob我不明白如何做到這一點 – hugo411

+0

@ the1nz4ne:然後堅持我建議的第二種方法。只需記住每個瓦片的起始位置,並保持最初爲零的偏移累加器。每次註冊移動事件時,將移動添加到累加器,並將每個貼片設置到原始位置加累加器。 –