简体   繁体   中英

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? 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,

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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