我有两个精灵:一个字符精灵,另一个是障碍精灵。障碍精灵是另一个精灵,称为bgSprite的小孩,它正在不断移动。我如何检测它们之间的碰撞。 我正在使用CGRECTINTESECTRECT,但它看起来不现实。我听说box2d,但我还没有使用它。下面是一些代码:如何通过cocos2d中的box2d检测碰撞
[[CCSpriteFrameCache sharedSpriteFrameCache]
addSpriteFramesWithFile:@"BoyRunAnimation.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:@"BoyRunAnimation.png"];
[self addChild:spriteSheet];
self._character = [CCSprite spriteWithSpriteFrameName:@"Boy_Run_0003.png"];
self._character.position = ccp(80, 150);
[spriteSheet addChild:self._character];
[self boyRunningAnimation];
//障碍
for (int i=0; i<5; i++)
{
int xPos=500+500*i;
if (xPos<2*_roadImage1.contentSize.width)
{
CCSprite *obstacle=[CCSprite node];
obstacle.textureRect=CGRectMake(0, 0, 50, _roadImage1.contentSize.height);
obstacle.color=ccc3(255, 255,255);
if (xPos <= _roadImage1.contentSize.width)
{
obstacle.position=ccp(xPos, _roadImage1.contentSize.height/2);
[_roadImage1 addChild:obstacle z:0 tag:1];
}
else
{
obstacle.position=ccp(xPos-_roadImage1.contentSize.width, 60);
[_roadImage2 addChild:obstacle z:0 tag:2];
}
[obstacleArray addObject:obstacle];
}
}
在更新方法:
CCRect obstacleBox = [obstacle boundingBox];
CCPoint obstaclePosition = obstacleBox.origin;
obstaclePosition = [[obstacle parent] convertToWorldSpace:obstaclePosition];
obstaclePosition = [[self._character parent] convertToNodeSpace:obstaclePosition];
obstacleBox.origin = obstaclePosition;
if (CGRectIntersectsRect(self._character.boundingBox, obstacleBox))
{
isTouchActive=NO;
NSLog(@"collision");
}
请帮助我。
检查Box2D的手册http://box2d.org/manual.pdf并遵循Box2D的教程:https://www.google.de/search?client=safari&rls = EN&q = cocos2d的+ Box2D的+教程&即= UTF-8&OE = UTF-8&gws_rd = CR EI = A1tVUpqbCa6p7Aam64HIBg#q =的cocos2d-iphone + Box2D的+教程&RLS = EN – LearnCocos2D