简体   繁体   English

在cocos2d中避免在透明部分中拖动精灵触摸

[英]Avoid dragging sprite touching in transparent part in cocos2d

I'm making an app that you can drag objects around the screen, but the object are not perfect squares so i'm trying to avoid transparent pixels. 我正在制作一个可以在屏幕上拖动对象的应用程序,但是该对象不是完美的正方形,因此我尝试避免使用透明像素。 Im using KKPixelMaskSprite class to do it, but it's not working properly Can anyone point me in the right direction? 我正在使用KKPixelMaskSprite类来执行此操作,但它无法正常工作有人可以向我指出正确的方向吗? I have a class that inherits from that class like following: 我有一个从该类继承的类,如下所示:

 @interface MyClass : KKPixelMaskSprite

And then i'm reading the touches with this: 然后我正在阅读与此相关的内容:

for (CCSprite *sprite in [_myLayer children]) {
        if ([sprite isKindOfClass:[MyClass class]]) {
            MyClass * object = (MyClass *)sprite;
            if (CGRectContainsPoint(object.boundingBox, touchLocation) && ![object pixelMaskContainsPoint:touchLocation]) {
                _selectedObject = object;
            }
        }
    }

And them moving _selectedObject around the screen, 然后他们在屏幕上移动_selectedObject,

The problem is that the selection is not perfect (it's not close to that), Does anyone have a solution ab that? 问题在于选择不完美(与之接近),有人能找到解决方案吗? is there any other path i can follow? 我还有什么其他路可走吗?

Thank you in advance 先感谢您

In this case you have to use pixel perfect collision. 在这种情况下,您必须使用像素完美碰撞。 Test the point that you touched is pixel of the sprite.. 测试您触摸的点是精灵的像素。

http://www.learn-cocos2d.com/2011/12/fast-pixelperfect-collision-detection-cocos2d-code-1of2/ http://www.learn-cocos2d.com/2011/12/fast-pixelperfect-collision-detection-cocos2d-code-1of2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM