检测cocos2d中sprite的透明部分? [英] Detect transparent part on the sprite in cocos2d?
问题描述
我是Cocos2d的初学者。我有一个sprite,我想忽略对该sprite的透明区域的触摸。
我知道这个答案 Cocos2d 2.0 - 忽略对层/ sprite的透明区域的触摸,精彩文章 http://www.learn-cocos2d.com / 2011/12 / fast-pixelperfect-collision-detection-cocos2d-code-1of2 / 。
我可以使用KKPixelMaskSprite,只有当sprite是从文件使用,而不是从批处理节点。每当我使用批处理节点(Sprite sheet),得到精灵,它停止工作。
我有不同的精灵对方,我想检测这种方式 - > if touch是在当前的sprite边框,是部分透明的sprite还是没有?
PSI'm使用cocos2d 1.0。我现在不想使用任何物理引擎,我只想忽略sprite透明区域(这是使用批处理节点创建的)。我该怎么办?
您可以使用CGMutablePathRef进行非矩形子画面碰撞检测。
//检查
CGPoint loc = [mySprite convertToNodeSpace:touchPoint];
if([mySprite isPointInsideMap:loc])
{
//接触内部..
}
//添加此方法在你的MySprite类派生自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);
I'm beginner in Cocos2d. I have a sprite, and I want to ignore touch on transparent area of that sprite.
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/.
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.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?
Thanks a lot in advance.
You can use CGMutablePathRef to make non-rectangular sprite collision detection.
//checking
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;
}
////Create Path
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中sprite的透明部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!