Cocos2D的CCMoveTo

时间:2011-04-20 17:12:00

标签: iphone cocos2d-iphone

我在场景中有两个CCSprite

CCSprite * spriteA; CCSprite * spriteB;

spriteB的位置是固定的,而spriteA的位置不是。我触摸spriteA,它只能以直线的方式移动,只有四个方向“上/下/左/右”,而不是斜线,也是它可以移动,直到spriteB停止它。比如我(spriteA)可以继续运行,直到我前面的墙(spriteB)阻止我。

我使用以下代码

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
[spriteA stopAllActions];
[spriteA runAction: [CCMoveTo actionWithDuration:1 position:convertedLocation]];

}

它可以被移动,但不仅仅是四个方向,如果spriteB位于spriteA的前面,我如何使用spriteB stop spriteA? 感谢

1 个答案:

答案 0 :(得分:0)

你的问题非常大,所以我会尝试在高层答复。

首先,你应该看一下手势识别器。对于cocos2D,您必须使用安装此特殊类:CCGestureRecognizer。这个类本质上是iOS上标准UIGestureRecognizer类的包装器。

您要使用的手势识别器是UISwipeGestureRecognizer。此手势识别器会告诉您何时在某个方向上检测到滑动。可用的四个方向是您正在寻找的方向。这四个方向定义为枚举:UISwipeGestureRecognizerDirection。当您注册UISwipeGestureRecognizer时,当用户在任何这些方向上滑动时,您将收到通知。

对于问题的第二部分,您需要进行碰撞检测。 Cocos2D有一个内置的物理引擎,可以帮助你解决这个问题,因为你的CCSprite形状大致是凸的。你可以找到great tutorial here on how to do collision detection in box2d here

我希望这能帮到你。