【问题标题】:CCMoveTo of Cocos2DCocos2D 的 CCMoveTo
【发布时间】:2011-04-20 17:11:59
【问题描述】:

我在现场有两个 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 停止 spriteA? 谢谢

【问题讨论】:

    标签: iphone cocos2d-iphone


    【解决方案1】:

    你的问题很大,所以我会尝试从高层次上回答。

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

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

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

    希望这对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      相关资源
      最近更新 更多