问题描述
我是 Cocos2d 的初学者.我有一个精灵,我想忽略对该精灵透明区域的触摸.
I'm beginner in Cocos2d. I have a sprite, and I want to ignore touch on transparent area of that sprite.
我知道这个答案 Cocos2d 2.0 - 忽略对图层/精灵透明区域的触摸,还有这篇很棒的文章 http://www.learn-cocos2d.com/2011/12/fast-pixelperfect-collision-detection-cocos2d-code-1of2/.
I'm aware of this answer Cocos2d 2.0 - Ignoring touches to transparent areas of layers/sprites, and also this great article http://www.learn-cocos2d.com/2011/12/fast-pixelperfect-collision-detection-cocos2d-code-1of2/.
我能够使其与 KKPixelMaskSprite 一起使用,但仅当从文件中使用 sprite 而不是从批处理节点使用时.每当我使用批处理节点(Sprite 表)来获取 sprite 时,它就会停止工作.
I was able to make it work with KKPixelMaskSprite, but only when sprite is used from file, but not from batch node. Whenever I use batch node (Sprite sheet), to get sprite , it stops working.
我有不同的精灵,我想用这种方式检测 -> 如果触摸在当前的精灵边界框中,那部分在精灵上是否透明?
I have different sprites on each other, and I want detect this way -> if touch is in current sprite bounding box, is that part transparent on sprite or no?
P.S.我正在使用 cocos2d 1.0.我现在不想使用任何物理引擎,我只想忽略对精灵透明区域的触摸(使用批处理节点创建).我该怎么做?或者是否有任何工具可以提供帮助?
P.S.I'm using cocos2d 1.0. I don't want to use any Physics engine for now, I just want to ignore touches on transparent areas of sprite (that was created using batch node ) .How can I do that? Or might there any tool can be helpfull?
非常感谢.
推荐答案
可以使用 CGMutablePathRef 进行非矩形精灵碰撞检测.
You can use CGMutablePathRef to make non-rectangular sprite collision detection.
//检查
CGPoint loc =[mySprite convertToNodeSpace:touchPoint];
if([mySprite isPointInsideMap:loc])
{
//touched inside..
}
//Add this method in your MySprite class derived from CCSprite.
-(bool)isPointInsideMap:(CGPoint)inPoint
{
if (CGPathContainsPoint(mCollisionPath, NULL, inPoint, NO) )
{
return true;
}
return false;
}
////创建路径
CGMutablePathRef mCollisionPath = CGPathCreateMutable();
CGPathMoveToPoint(mCollisionPath, NULL, 0, 0 );
CGPathAddLineToPoint(mCollisionPath, NULL, 11, 82 );
CGPathAddLineToPoint(mCollisionPath, NULL, 42, 152 );
CGPathAddLineToPoint(mCollisionPath, NULL, 86, 202 );
CGPathAddLineToPoint(mCollisionPath, NULL, 169, 13 );
CGPathCloseSubpath(mCollisionPath);
这篇关于在cocos2d中检测精灵上的透明部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!