【问题标题】:Accessing the texture's rectangle in a cocos2d sprite在 cocos2d 精灵中访问纹理的矩形
【发布时间】:2013-11-09 21:38:47
【问题描述】:

我有CCSprite,它的边缘有一些填充物(出于我将在这里略过的原因)。我正在从精灵表创建精灵,并且它是连续动画的。在这里,我添加了一个半透明的蓝色精灵来显示精灵的contentSize。我还打开了CC_SPRITE_DEBUG_DRAW 以便在(两个)精灵周围绘制边框:

因此,蓝色框代表CCSpriteboundingBox / contentSize 属性。质地。这是正确的、理想的功能。

但是...如您所见,CC_SPRITE_DEBUG_DRAW 能够识别绘制纹理的实际边缘。我想访问实际的“绘制区域”(例如,作为CGRect)。换句话说:我希望能够检测用户是否触摸了设备上,而不是简单地触摸蓝色框 (boundingBox)。

如何访问此CGRect

【问题讨论】:

    标签: ios iphone cocos2d-iphone


    【解决方案1】:

    查看调试绘制代码我发现了这个:

    #if CC_SPRITE_DEBUG_DRAW == 1
        // draw bounding box
        CGPoint vertices[4]={
            ccp(_quad.tl.vertices.x,_quad.tl.vertices.y),
            ccp(_quad.bl.vertices.x,_quad.bl.vertices.y),
            ccp(_quad.br.vertices.x,_quad.br.vertices.y),
            ccp(_quad.tr.vertices.x,_quad.tr.vertices.y),
        };
        ccDrawPoly(vertices, 4, YES);
    #elif CC_SPRITE_DEBUG_DRAW == 2
        // draw texture box
        CGSize s = self.textureRect.size;
        CGPoint offsetPix = self.offsetPosition;
        CGPoint vertices[4] = {
            ccp(offsetPix.x,offsetPix.y), ccp(offsetPix.x+s.width,offsetPix.y),
            ccp(offsetPix.x+s.width,offsetPix.y+s.height), 
                ccp(offsetPix.x,offsetPix.y+s.height)
        };
        ccDrawPoly(vertices, 4, YES);
    #endif // CC_SPRITE_DEBUG_DRAW
    

    看起来你可以从精灵的quad 属性中得到你想要的东西。或者可能是第二种解决方案,因为我不知道cocos2d这里的bounding box和texture box是什么意思。

    【讨论】:

    • 我已经花了一段时间来解决这个问题并且到目前为止运气不佳,这就是为什么我最终在这里发帖希望有一个简单的解决方案......不过感谢提示!
    【解决方案2】:

    这是我想出的代码,作为CCSprite 的自定义子类中的一个函数:

    // In local space
    - (CGRect)hitArea {
      CGPoint bl = CGPointMake(MIN(_quad.tl.vertices.x, _quad.bl.vertices.x), MIN(_quad.bl.vertices.y, _quad.br.vertices.y));
      CGPoint tr = CGPointMake(MAX(_quad.tr.vertices.x, _quad.br.vertices.x), MAX(_quad.tl.vertices.y, _quad.tr.vertices.y));
      return CGRectMake(bl.x, bl.y, tr.x - bl.x, tr.y - bl.y);
    }
    
    // In game space, like how .boundingBox works
    - (CGRect)hitBox {
      return CGRectApplyAffineTransform(self.hitArea, [self nodeToParentTransform]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多