Cocos2d对我而言是新鲜的,所以我不知道我应该对这种情况做些什么:
我想制作类似风险的游戏。现在我制作了一张像世界地图一样的背景图片(只是为了测试)。并且在这张地图上我想要一个轻扫手势,这样我就可以在我的ipad上移动地图(地图很大,所以我想在它周围滑动)。
我的问题是我不知道我应该使用的对象叫什么。以及如何以最佳方式实现手势(我是否需要自己计算动作?)。
谢谢! 斯蒂芬。
答案 0 :(得分:3)
您可以将UIKit的Pan Gesture Recognizer连接到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;
}
}
答案 1 :(得分:-1)
我认为你正在寻找这个来添加一个对象:
CCSprite *objectName = [CCSprite spriteWithFile:@"fileName.png"];
[self addChild:objectName];
默认情况下,我相信该对象位于左下角。