2015-11-01 122 views
2

我有两个节点。球员和敌人。我希望Enemy节点在Player节点足够近时跟随Player节点,并且当它与Player节点发生冲突时,Enemy节点将停止。我得到的是Enemy节点位于Player之上,并且两个节点都被推送。我想过要以某种方式阻止Enemy节点与玩家碰撞,但在我看来,它应该是一个清洁器的方式。Sprite Kit中的简单碰撞

(我通过在更新中更改它的位置来移动敌人节点)。

这里是我的GameScene.sks:

character and enemy options from GameScene

-(void)didMoveToView:(SKView *)view { 

    player = [self childNodeWithName:@"character"]; 
    enemy = [self childNodeWithName:@"enemy"]; 
    self.physicsWorld.contactDelegate = self; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    for (UITouch *touch in touches) { 
     moveToTouch = [touch locationInNode:self]; 
     SKNode *tile = [self nodeAtPoint:moveToTouch]; 
     if([tile.name isEqualToString:@"tile"]) 
     { 
      moveToTouch = tile.position; 
     } 
    } 
} 

-(void)update:(CFTimeInterval)currentTime { 

    [self walk:player to:moveToTouch]; 

    if((SDistanceBetweenPoints(enemy.position, player.position) < 200)) 
    { 
     [self walk:enemy to:player.position]; 
    } 


} 

-(void)walk:(SKNode*)node to:(CGPoint)moveTo 
{ 
    if (CGPointEqualToPoint(moveTo, CGPointZero)) 
    { 
     return; 
    } 


    if(round(moveTo.y) != round(node.position.y)) 
    { 

     if(moveTo.y > node.position.y) 
     { 
      node.position = CGPointMake(node.position.x,node.position.y+2); 
     } 
     else if (moveTo.y < node.position.y) 
     { 
      node.position = CGPointMake(node.position.x,node.position.y-2); 
     } 

    }else if(round(moveTo.x) != round(node.position.x)) 
    { 

     if(moveTo.x > node.position.x) 
     { 
      node.position = CGPointMake(node.position.x+2,node.position.y); 
     } 
     else if (moveTo.x < node.position.x) 
     { 
      node.position = CGPointMake(node.position.x-2,node.position.y); 
     } 

    } 

    float distance = SDistanceBetweenPoints(node.position, moveTo); 
    if (distance < 1.0){ 
     moveToTouch = CGPointZero; 
    } 

} 
+0

我从来没有使用Visual组件制作精灵套件游戏,但它听起来像你的问题是你不想注册碰撞,所以设置这些掩码为0(或者让他们有一个面具,他们可以' t碰撞)。然后你要专注于使用你的接触面罩,并在他们联系的地方,阻止敌人 – Knight0fDragon

+0

@ Knight0fDragon谢谢,我会尽力的! – HelloimDarius

+1

应该使用您的场景中的联系人代表与dedbegincontact函数调用 – Knight0fDragon

回答

1

我不知道你是怎么把它设置你的敌人后面的球员。 但是你可以尝试

  • 设置的敌人动态假
  • 或把速度0

使用碰撞检测,知道什么时候该相撞。

+0

将velocity设置为0(可从图像中看到)。我要用能让敌人跟随玩家的代码编辑我的第一篇文章。 – HelloimDarius

+0

volicity设置为0是因为它们相撞时。图像只是呈现了节点的开始状态 – Cing

+0

当我仔细观察图像时,看到了像网格一样的锯齿。因此,你的玩家和敌人的网格(不知道这个图像是隐秘的 – Cing