我已经被困了好几个星期了,现在试图找出如何忽略对精灵透明区域的触摸。我一直在尝试使用本教程来跟踪像素完美碰撞——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/无济于事。这是我的代码目前的样子:
-(void)checkTap:(CGPoint)touch{
BOOL yesNo = NO;
if(yesNo == NO)
{
sprTap.position = ccp(touch.x, touch.y);
}}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
[self checkTap:location];
touchFlag = 0;
for(int i = 0; i < [sprArray count]; i++)
{
KKPixelMaskSprite *sprite = (KKPixelMaskSprite *)[sprArray objectAtIndex:i];
if([sprTap intersectsNode:sprite])
{
selectedSprite = sprite;
touchFlag = 1;
break;
}
}}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
[sprTap setPosition:location];
if(touchFlag == 1)
{
_spriteTouch = TRUE;
[selectedSprite setPosition:location];
}
else
{
for(int i = 0; i < [sprArray count]; i++)
{
KKPixelMaskSprite *sprite = (KKPixelMaskSprite *)[sprArray objectAtIndex:i];
if([sprTap intersectsNode:sprite])
{
selectedSprite = sprite;
touchFlag = 1;
break;
}
}
}
}}
问题是,当 sprTap 的边界框与精灵的边界框相交时,它会同时移动,并且由于我的精灵不是完美的正方形,所以这是行不通的。我也尝试过 PixelMaskIntersectsNode 但这似乎也不起作用。我还能如何忽略对精灵透明部分的触摸?请帮我。
您可以使用 CGMutablePathRef 来检测透明部分:
参考我的回答这个线程。 https://stackoverflow.com/questions/12333822/detect-transparent-part-on-the-sprite-in-cocos2d/12516903#12516903
有关如何创建 CGPath 的信息:Here https://stackoverflow.com/questions/14953630/cgpath-is-not-detected-properly-in-cocos2d/14954003#14954003
了解更多信息,点击这里 http://bobueland.com/cocos2d/2011/the-magic-of-cgpaths/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)