【问题标题】:cocos2d sprite collision detectioncocos2d sprite 碰撞检测
【发布时间】:2012-03-22 13:19:08
【问题描述】:

我目前正在使用 cocos2d 为 iphone 创建一个点击应用程序。然而,在我当前的实现中,碰撞检测条件只有在我点击左上角而不是精灵中的任何地方时才成立。如果我将锚点位置设置为 0,这会使事情变得更好,但这会导致我的旋转精灵功能中断。

这是我的代码,有人能发现这里有什么问题吗? 在我的初始化代码中

if( (self=[super init])) 
{       

        cocosGuy = [TouchableSprites spriteWithFile: @"Icon.png"];  
        //[cocosGuy setAnchorPoint:CGPointMake(0, 0)];
        cocosGuy.position = ccp( 200, 300 );
    //[cocosGuy setPosition: ccp(100,100)];
        [self addChild:cocosGuy];
    self.isTouchEnabled = YES;
}

在 touchBegan 中,我确定图像是否被触摸

    CGPoint pt = [touch locationInView:[touch view]];
    CGPoint ptConv = [[CCDirector sharedDirector] convertToGL:pt];

CGSize size = [cocosGuy contentSize];
CGPoint point = [cocosGuy position];
CGRect rect = CGRectMake(point.x, point.y, size.width, size.height); 

if (CGRectContainsPoint(rect, ptConv))
{
    retValue = YES;
}   

任何帮助将不胜感激

【问题讨论】:

    标签: iphone objective-c cocos2d-iphone collision-detection sprite


    【解决方案1】:

    您正在生成的矩形获取触摸点,然后将该点作为角点的矩形。

    CCSprite 的原点位于图像的中间。所以你需要在做直肠时考虑到这一点

    CGRect rect = CGRectMake(point.x - (size.width / 2), point.y - (size.height / 2), size.width, size.height);

    【讨论】:

      【解决方案2】:

      要处理旋转,最好的方法是将触摸点转换为精灵局部空间(这也将正确处理缩放和精灵层次结构)

      CGPoint ptConv = [cocosGuy convertTouchToNodeSpace: touch];
      if (CGRectContainsPoint ([cocosGuy boundingBox], ptConv)
      {
          retValue = YES;
      }
      

      【讨论】:

      • 谢谢各位,我在你们的帮助下整理出来了,这条线也没有帮助旋转,摆脱它解决了部分问题
      • [selSprite runAction:[CCRotateTo actionWithDuration:0.1 angle:0]];
      猜你喜欢
      • 2011-08-14
      • 1970-01-01
      • 2014-01-09
      • 2014-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多