在我的游戏中碰撞的更好的方法是什么?

时间:2014-12-09 08:00:28

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

当角色与障碍物碰撞时,游戏结束。它在技术上有效,但我需要解决的问题是角色能够与障碍重叠,甚至在没有游戏结束的情况下逃脱。两者需要真正重叠才能引起游戏结束。

现在我认为问题可能是因为它们在同一位置发生碰撞。如果他们必须拥有相同的位置,那就意味着他们center point / pixel见面的时候,因为这个位置究竟是什么?它是图像的中心还是图像的边缘pixels

我真的很挣扎,想要一些帮助。谢谢!

//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)
    //if(flyCopter.position.y <= blockTmp2.position.y + blockTmp2.size.height)
    //if(flyCopter.position.x <= blockTmp2.position.x + blockTmp2.size.width)
    //if(flyCopter.position.y <= blockTmp3.position.y + blockTmp3.size.height)
    //if(flyCopter.position.x <= blockTmp3.position.x + blockTmp3.size.width)
    //if(flyCopter.position.y <= blockTmp4.position.y + blockTmp4.size.height)
    //if(flyCopter.position.x >= blockTmp4.position.x + blockTmp4.size.width)
    {
        [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 :(得分:-1)

如果你使用Sprite Kit,那么就可以利用SKPhysicsBody和SKPhysicsWorld。

首先阅读Apple&#39:Sprite Kit Guide

另请参阅本教程中的碰撞:Space Invaders - Part 1