<tfoot id='sZtXg'></tfoot>
  • <small id='sZtXg'></small><noframes id='sZtXg'>

    <i id='sZtXg'><tr id='sZtXg'><dt id='sZtXg'><q id='sZtXg'><span id='sZtXg'><b id='sZtXg'><form id='sZtXg'><ins id='sZtXg'></ins><ul id='sZtXg'></ul><sub id='sZtXg'></sub></form><legend id='sZtXg'></legend><bdo id='sZtXg'><pre id='sZtXg'><center id='sZtXg'></center></pre></bdo></b><th id='sZtXg'></th></span></q></dt></tr></i><div id='sZtXg'><tfoot id='sZtXg'></tfoot><dl id='sZtXg'><fieldset id='sZtXg'></fieldset></dl></div>
      <legend id='sZtXg'><style id='sZtXg'><dir id='sZtXg'><q id='sZtXg'></q></dir></style></legend>
          <bdo id='sZtXg'></bdo><ul id='sZtXg'></ul>

      1. Cocos2d iPhone 非矩形精灵触摸检测

        时间:2024-08-12
        <legend id='sq1XG'><style id='sq1XG'><dir id='sq1XG'><q id='sq1XG'></q></dir></style></legend>

          • <i id='sq1XG'><tr id='sq1XG'><dt id='sq1XG'><q id='sq1XG'><span id='sq1XG'><b id='sq1XG'><form id='sq1XG'><ins id='sq1XG'></ins><ul id='sq1XG'></ul><sub id='sq1XG'></sub></form><legend id='sq1XG'></legend><bdo id='sq1XG'><pre id='sq1XG'><center id='sq1XG'></center></pre></bdo></b><th id='sq1XG'></th></span></q></dt></tr></i><div id='sq1XG'><tfoot id='sq1XG'></tfoot><dl id='sq1XG'><fieldset id='sq1XG'></fieldset></dl></div>
                <bdo id='sq1XG'></bdo><ul id='sq1XG'></ul>

                <small id='sq1XG'></small><noframes id='sq1XG'>

              • <tfoot id='sq1XG'></tfoot>
                  <tbody id='sq1XG'></tbody>

                • 本文介绍了Cocos2d iPhone 非矩形精灵触摸检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 sprite 中有一个带有三角形图形的项目.我将这些精灵排列在一个网格中,以便它们的矩形都重叠.当精灵被触摸时,它们的 z-order 正在被更改(由我)以将它们放在 zOrder 的顶部.

                  Have a project with a TRIANGLE shaped graphic in a sprite. I am arranging these sprites in a grid so that their rectangles are all overlapping. As sprites get touched their z-order is being changed (by me) to put them on the top of zOrder.

                  我正在使用 Cocos 0.8.1 和 touch dispatcher 方法.我有触摸工作,但显然无法触摸与其他精灵重叠的隐藏"精灵.

                  I am using Cocos 0.8.1 and the touch dispatcher method. I have touches working but obviously the "hidden" sprites which are overlapped by other sprites are not able to be touched.

                  问题是Is the touch in my rect"方法是基于精灵的矩形,但是图像是三角形,请问有没有人知道一个Cocos友好的测试方法是否图像本身正在被击中.

                  The problem is that the "Is the touch in my rect" method is based on the rectangle of the sprite, but the image is a triangle, and I would like to ask if anyone knows a Cocos-friendly method of testing whether the image itself is being hit.

                  我似乎记得这是当时流行的命中测试方法,但我找不到任何关于如何在 Cocos/iPhone Land 中完成它的参考.

                  I seem to remember this was a popular method of hit testing back in the day but I can't find any reference to how it might be done in Cocos/iPhone Land.

                  目标是仅在触摸图像像素时响应触摸,而不仅仅是包含精灵的矩形.

                  The goal is to only respond to touches when an image pixel is touched, not just the rect containing the sprite.

                  推荐答案

                  您当然可以测试任何您想要的形状,而不是对盒子进行测试.

                  Instead of testing against a box you can of course test against any shape that you wish.

                  我最初发布了您可以使用 NSBezierPath,但显然 iPhone 套件上不可用,只能在 Mac 上使用.相反,在 iPhone 上您可以使用 CGPath.

                  I initially posted you can use an NSBezierPath, but appearantly that ins't available on the iPhone kit, only on Macs. Instead, on the iPhone you can use CGPath.

                  使用 CGPathCreateMutable() 创建一个新路径,该路径返回一个 const CGPath *(也称为 CHPathRef
                  然后使用 CGPathAddRectCGPathAddLines 创建我们的路径.
                  CGPathContainsPoint 将测试您的点是否在形状中.

                  Create a new path by using CGPathCreateMutable() which returns a const CGPath * (also known as CHPathRef
                  Then use CGPathAddRect or CGPathAddLines to create our path.
                  CGPathContainsPoint will test if your point was in the shape.

                  或者,您可以创建一个客户函数,该函数(因为您使用三角形)只进行简单的计算以检查您的点是否在三角形内.一点数学应该可以解决问题(虽然当你旋转形状时它会稍微复杂一些.我稍微写一下,因为你可以相对于形状的原点旋转触摸点并进行命中检测)

                  Alternatively, you can create a customer function that (since you use triangles) just does a simple calculation to check whether your point is inside the triangle shape. Bit of math should do the trick (though when you rotate the shape it'll be slightly more complicated. I write slightly, as you can then just rotate the touch point relative to the shape's origin and do the hit detection)

                  对于三角形:

                     C
                    /
                   /__
                  A    B
                  point of touch is P
                  

                  通过以下算法,您应该能够找到触摸:

                  With the following algorithm you should be able to find the touch:

                  /* first test against a box, for performance */
                  if( P.y > C.y || P.y < A.y || P.x < A.x || P.X > B.x )
                      return false; // false if P falls outside "the box"
                  
                  /* then check if its within the shape */
                  /* split the triangle into 2 parts, around the axle of point C */
                  if( P.x < C.x ) // if the x value of point P is on the left of point C
                      if( P.y > ((C.y -A.y) / (C.x - A.x)) * P.x )
                          return false; // the point is above the triangle's side AC
                  else // if the x value of point P is greater than or equal to point C
                      if( P.y > C.y - ((C.y - B.y) / ( B.x - C.x )) * ( P.x - C.x ) )
                          return false; // the point is above the triangle's side BC
                  
                  return true; // the point must be in the triangle, so return true.
                  

                  以上是干编码,但应该是正确的.

                  The above is dry coded, but should be correct.

                  以上内容仅适用于我绘制的形状中的三角形(其中 C.x 位于 A.x 和 B.x 之间,A 和 B 处于相同高度,但低于 C).当然,您可以修改它以针对任何形状进行测试,但是,您必须权衡仅使用可用的 CGPath.

                  The above only works against a triangle in the shape as I drew it (where C.x is between A.x and B.x, and A and B are on the same height, but below C). Of course you can modify this to test against any shape, however, you have to weigh this against just using the available CGPath.

                  如果你不明白,或者它有问题,请告诉我!

                  If you don't get it, or if it's faulty, let me know!

                  这篇关于Cocos2d iPhone 非矩形精灵触摸检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:多点触控检测差异化 - Cocos2d for iPhone 下一篇:在 Cocos2d-iPhone 中围绕圆周移动精灵

                  相关文章

                    <bdo id='w3arS'></bdo><ul id='w3arS'></ul>
                  <legend id='w3arS'><style id='w3arS'><dir id='w3arS'><q id='w3arS'></q></dir></style></legend>
                  1. <i id='w3arS'><tr id='w3arS'><dt id='w3arS'><q id='w3arS'><span id='w3arS'><b id='w3arS'><form id='w3arS'><ins id='w3arS'></ins><ul id='w3arS'></ul><sub id='w3arS'></sub></form><legend id='w3arS'></legend><bdo id='w3arS'><pre id='w3arS'><center id='w3arS'></center></pre></bdo></b><th id='w3arS'></th></span></q></dt></tr></i><div id='w3arS'><tfoot id='w3arS'></tfoot><dl id='w3arS'><fieldset id='w3arS'></fieldset></dl></div>

                    <small id='w3arS'></small><noframes id='w3arS'>

                    1. <tfoot id='w3arS'></tfoot>