2012-11-26 80 views
1

的cocos2d對我來說是新的prety,所以我不知道我應該有這種情況做:科科斯2D背景圖的手勢

我想打遊戲那類似風險。現在我製作了一張像世界地圖的背景圖片(僅供測試)。並在這張地圖上我想要一個輕掃手勢,所以我可以在地圖上移動我的ipad(地圖是prety大,所以我想要刷到它周圍)。

我的問題是我不知道什麼對象被稱爲我應該使用。我如何以最好的方式實現手勢(我是否需要自己計算運動?)。

謝謝! Stefan。

回答

-1

我認爲你正在尋找這個添加的對象:

CCSprite *objectName = [CCSprite spriteWithFile:@"fileName.png"]; 
    [self addChild:objectName]; 

默認情況下,我認爲對象會在左下角。

3

您可能可以將UIKit的Pan Gesture識別器連接到CCDirector的視圖並處理CCLayer類中的平移手勢。通過這種方式,您可以處理隨每次平移運動而移動背景的方法。 (代碼cocos2d的1.0.1,類似的可與2.0版本實現)

UIPanGestureRecognizer* pan = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)] autorelease]; 
CCDirector* director = [CCDirector sharedDirector]; 
[[director openGLView] addGestureRecognizer:pan]; 

處理方法是這樣的:

- (void)handlePanGesture:(UIGestureRecognizer*)gestureRecognizer { 
// If there is more than one pan gesture recognizer connected with this method, you should remember pan and check if gestureRecognizer is equal to pan 

switch (gestureRecognizer.state) { 
    case UIGestureRecognizerStateBegan: { 

     // Do something that needs to be done when pan gesture started 

     break; 
    } 
    case UIGestureRecognizerStateChanged: { 

     // Get pan gesture recognizer translation 
     CGPoint translation = [(UIPanGestureRecognizer*)gestureRecognizer translationInView:gestureRecognizer.view]; 

     // Invert Y since position and offset are calculated in gl coordinates 
     translation = ccp(translation.x, -translation.y); 

     // Here you should move your background, probably in oposite direction of translation vector, something like 
     background.position = ccp(background.position.x - translation.x, background.position.y - translation.y); 

     // Refresh pan gesture recognizer 
     [(UIPanGestureRecognizer*)gestureRecognizer setTranslation:CGPointZero inView:gestureRecognizer.view]; 

     break; 
    } 
    case UIGestureRecognizerStateEnded: { 

     // Do some work that should be done after panning is finished 

     break; 
    } 
    default: 
     break; 
    } 
}