有没有更好的方法来创建两个对象之间的碰撞?

时间:2014-12-05 05:55:57

标签: objective-c sprite-kit collision-detection

我创造的角色经常与导致游戏结束的障碍重叠。角色是flyCopter,它是构成整个角色的一组png。 blockTmp是由png组合构成的障碍。

有时角色没有碰撞,你可以在没有游戏结束的情况下离开,但显然它不应该重叠。当它们两个立即发生碰撞时应该结束游戏。奇怪的是,有时候它会死掉"死亡"在接触,然后其他时间它真的需要重叠。

谢谢

// CHARACTER COLLISION

-(void)checkGameOver{
if(GAMESTART){
if(flyCopter.position.x >= CGRectGetMaxX(self.frame) - flyCopter.copterWidth/2 - 6){
  [self showOver];
}

if(flyCopter.position.x <= CGRectGetMinX(self.frame) + flyCopter.copterWidth/2 + 6){
  [self showOver];
}

[self compareCP:blockTmp1 :flyCopter];
[self compareCP:blockTmp2 :flyCopter];
if(flyCopter.position.y <= blockTmp1.position.y - 0 + blockTmp1.size.height + 0)
if(flyCopter.position.x <= blockTmp1.position.x +20 + blockTmp1.size.width + 0)

{
    [self compareCP:blockTmp3 :flyCopter];
    [self compareCP:blockTmp4 :flyCopter];
}

[self compareCP:blockTmp5 :flyCopter];
[self compareCP:blockTmp6 :flyCopter];
if(flyCopter.position.y <= blockTmp5.position.y - 0 + blockTmp5.size.height + 0)
if(flyCopter.position.x <= blockTmp5.position.x +20 + blockTmp5.size.width + 0)

{
    [self compareCP:blockTmp7 :flyCopter];
    [self compareCP:blockTmp8 :flyCopter];
}

[self compareCP:blockTmp9 :flyCopter];
[self compareCP:blockTmp10 :flyCopter];
if(flyCopter.position.y <= blockTmp9.position.y - 0 + blockTmp9.size.height + 0)
  if(flyCopter.position.x <= blockTmp9.position.x +20 + blockTmp9.size.width + 0)

{
    [self compareCP:blockTmp11 :flyCopter];
    [self compareCP:blockTmp12 :flyCopter];
  }
}
}


-(void)compareCP:(SKSpriteNode*)flyCopters : (SKSpriteNode*)blocks{
if(flyCopters){
    if ([blocks intersectsNode:flyCopters]) {
        [self showOver];
    }
}
}

1 个答案:

答案 0 :(得分:0)

使用Box2d进行更好的碰撞。