我正在试图弄清楚如何做到这一点,我有一个名为'玩家'的精灵和名为'摇滚'的精灵 我正试图检测两个精灵之间的碰撞.....但碰撞没有任何反应!
继续我所做的事情:-(void)addRock {
rock = [CCSprite spriteWithFile:@"rock.png"
rect:CGRectMake(0, 0, 27, 40)];
// Determine where to spawn the target along the X axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = rock.contentSize.width/2;
int maxX = winSize.width - rock.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;
// Create the target slightly off-screen along the right edge,
// and along a random position along the X axis as calculated above
rock.position = ccp(actualX, 500);
rock.tag = 1;
[rockArray addObject:rock];
[self addChild:rock];
以下播放器代码:
-(id) init{
if( (self=[super initWithColor:ccc4(255, 255, 255, 255)] )) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
player = [CCSprite spriteWithFile:@"Player.png"
rect:CGRectMake(0, 0, 27, 40)];
player.position = ccp(winSize.width/2, winSize.height/4+15); //position of where the player is placed
player.tag = 2;
[playerArray addObject:player];
[self addChild:player];
精灵移动完成:
-(void)spriteMoveFinished:(id)sender {
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];
if (sprite.tag == 1) { // rock
[rockArray removeObject:sprite];
} else if (sprite.tag == 2) { // players
[playerArray removeObject:sprite];
init(数组初始化):
rockArray = [[NSMutableArray alloc]init];
playerArray = [[NSMutableArray alloc]init];
[self schedule:@selector(update:)];
更新方法
- (void)update:(ccTime)dt {
NSMutableArray *rocksToDelete = [[NSMutableArray alloc] init];
for (rock in rockArray) {
CGRect rockRect = CGRectMake(
rock.position.x - (rock.contentSize.width/2),
rock.position.y - (rock.contentSize.height/2),
rock.contentSize.width,
rock.contentSize.height);
NSMutableArray *playersToDelete = [[NSMutableArray alloc] init];
for (player in playerArray) {
CGRect pRect = CGRectMake(
player.position.x - (player.contentSize.width/2),
player.position.y -(player.contentSize.height/2),
player.contentSize.width,
player.contentSize.height);
if (CGRectIntersectsRect(rockRect, pRect)) {
[rocksToDelete addObject:rock];
}
for (rock in rocksToDelete) {
[rockArray removeObject:rock];
[self removeChild:rock cleanup:YES];
}
if (rocksToDelete.count > 0) {
[playersToDelete addObject:player];
}
[rocksToDelete release];
for (player in playersToDelete) {
[playerArray removeObject:player];
[self removeChild:player cleanup:YES];
}
[playersToDelete release];
我一直在努力整理我的问题。我似乎可以让碰撞检测工作。如果是这样,有人可以给我一个简短的代码吗?
答案 0 :(得分:0)
要检查两个精灵之间的碰撞,你必须每帧调用一个方法(或者你经常选择的方式)来检查是否发生了碰撞。 将以下代码添加到init方法:
[self scheduleUpdate];
...这将在每一帧上调用以下方法(您必须添加到类实现中):
-(void)update:(ccTime)delta {
if (CGRectIntersectsRect(player.boundingBox, rock.boundingBox)) {
//Do Stuff
}
}
对于以前的代码,你必须将Player和Rock精灵声明为成员变量,以便在整个班级中使用它们。
如果场景中有多个岩石精灵,你想要检查碰撞,你将不得不使用一个阵列。快速举例:
1在类接口中声明一个数组:
NSMutableArray *rockArray;
2在init方法中为它分配内存
rockArray = [NSMutableArray array];
3在“addRock”方法中,将创建的岩石精灵添加到数组中,然后将其作为子项添加到场景中
-(void) addRock {
CCSprite *rock = [CCSprite spriteWithFile:@"rock.png"
rect:CGRectMake(0, 0, 27, 40)];
//...
[rockArray addObject:rock];
[self addChild:[rockArray lastObject]];
}
4在“更新”方法中,您现在循环遍历数组并检查rock& amp;播放器
for (CCSprite *_rock in rockArray) {
if (CGRectIntersectsRect(player.boundingBox, rock.boundingBox)) {
//Do Stuff e.g remove that rock from the scene
[self removeChild:_rock cleanup:YES];
}
}
希望这有帮助;-)随意提出任何问题