1

Cocos2d 对我来说很新,所以我不知道我应该如何处理这种情况:

我想做一款冒险的游戏。现在我制作了一个像世界地图一样的背景图像(只是为了测试)。在这张地图上,我想要一个滑动手势,这样我就可以在我的 ipad 上的地图上移动(地图很大,所以我想左右滑动它)。

我的问题是我不知道我应该使用什么对象。以及如何以最佳方式实现手势(我需要自己计算运动吗?)。

谢谢!斯特凡。

4

2 回答 2

3

您可以将 UIKit 的平移手势识别器连接到 CCDirector 的视图并在 CCLayer 类中处理平移手势。这样,您就可以使用 handle 方法,该方法会随着每次平移移动而移动背景。(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;
    }
}
于 2012-11-28T19:14:16.867 回答
-1

我想你正在寻找这个来添加一个对象:

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

默认情况下,我相信对象会在左下角。

于 2012-11-26T20:03:55.173 回答