2016-07-25 103 views
1

这里是我的问题:障碍随着GameplayKit

  • 我如何转换的SKSpriteNode数组的GKObstacles阵列,使得代理人可以适当避免这些障碍通过使用goalToAvoidObstacles:(nonnull NSArray<GKObstacle *> *) maxPredictionTime:(NSTimeInterval) /?

看来,以何种方式,我创建了GKObstacle阵列不允许GKGoal正确识别这些的障碍,不管我给的目标是什么重量。

我目前有几个SKNode s被添加到SKScenechapterScene。这些节点中的每一个都被添加到我正在存储的名为障碍阵列的阵列中。我已成立了以下列方式工作得很好的测试:

WORKING障碍

- (void)didMoveToView:(nonnull SKView *)view { 
    [super didMoveToView:view]; 

    // Add three obstacles in a triangle formation around the center of the scene. 
    NSArray<GKObstacle *> *obstacles = @[ 
             [self addObstacleAtPoint:CGPointMake(CGRectGetMidX(self.frame), 
                       CGRectGetMidY(self.frame) + 150)], 
             [self addObstacleAtPoint:CGPointMake(CGRectGetMidX(self.frame) - 200, 
                       CGRectGetMidY(self.frame) - 150)], 
             [self addObstacleAtPoint:CGPointMake(CGRectGetMidX(self.frame) + 200, 
                       CGRectGetMidY(self.frame) - 150)], 
             ]; 

    // The player agent follows the tracking agent. 
    self.player = [[OPlayer alloc] initWithScene:self 
               radius:50 
               position:CGPointMake(CGRectGetMidX(self.frame), 
                    CGRectGetMidY(self.frame))]; 
    self.player.agent.behavior = [[GKBehavior alloc] init]; 
    [self.agentSystem addComponent:self.player.agent]; 

    // Create the seek goal, but add it to the behavior only in -setSeeking:. 
    self.seekGoal = [GKGoal goalToSeekAgent:self.character]; 

    // Add an avoid-obstacles goal with a high weight to keep the agent from overlapping the obstacles. 
    [self.player.agent.behavior setWeight:100 forGoal:[GKGoal goalToAvoidObstacles:obstacles maxPredictionTime:1]]; 

} 

- (GKObstacle *)addObstacleAtPoint:(CGPoint)point { 
    SKShapeNode *circleShape = [SKShapeNode shapeNodeWithCircleOfRadius:50]; 
    circleShape.lineWidth = 2.5; 
    circleShape.fillColor = [SKColor grayColor]; 
    circleShape.strokeColor = [SKColor redColor]; 
    circleShape.zPosition = 1; 
    circleShape.position = point; 
    [self addChild:circleShape]; 

    GKCircleObstacle *obstacle = [GKCircleObstacle obstacleWithRadius:50]; 
    obstacle.position = (vector_float2){point.x, point.y}; 

    return obstacle; 
} 

虽然这工作得很好,我遇到的问题是,我不能让行为确定我作为障碍物的SKNode尸体阵列。我只能将这些手动创建的GKCircleObstacle项目注册为代理程序避免的有效障碍。这是一个问题的原因是因为我依赖许多事实上并非简单圆圈的障碍,但多边形结构有些复杂,有些更直接。不过,我正尝试以下,但我的经纪人似乎没有避免的是的SKNode这样的障碍:

不工作障碍

NSArray *obstacles = [SKNode obstaclesFromNodePhysicsBodies:obstaclesArray]; 

/* 
The other code seen above 
*/ 

// Add an avoid-obstacles goal with a high weight to keep the agent from overlapping the obstacles. 
[self.player.agent.behavior setWeight:100 forGoal:[GKGoal goalToAvoidObstacles:obstacles maxPredictionTime:1]]; 

不幸的是,这似乎并没有工作因为无论我把重量设置得有多高,代理人都不会回避避开障碍物。下面是一个日志我传递给它的数组:

2016-07-24 22:32:24.907 <GAME>[1516:475581] obstacles: (
    "<GKPolygonObstacle: 0x14d20d70>", 
    "<GKPolygonObstacle: 0x14d20e10>", 
    "<GKPolygonObstacle: 0x14d20ba0>", 
    "<GKPolygonObstacle: 0x14d20bb0>", 
    "<GKPolygonObstacle: 0x14d20bc0>", 
    "<GKPolygonObstacle: 0x14d20a60>", 
    "<GKPolygonObstacle: 0x14d208b0>", 
    "<GKPolygonObstacle: 0x14d207d0>", 
    "<GKPolygonObstacle: 0x14d20a70>" 
) 

其中每一种都被明确并适当地初始化,并添加到场景。这些元素中的每一个实际上都对SpriteKitdidBeginContact:(SKPhysicsContact *)contact方法有反应,所以看起来节点正确地处理它们应该具有的边界。

如果有人知道我在做什么错误或更好的方法将这些'障碍节点'转换为GKObstacle'我会非常感激。提前致谢!

UPDATE:这是一个物理的身体的一个节点,我测试:

 SKSpriteNode *innerMap1 = [SKSpriteNode spriteNodeWithImageNamed:@"test"]; 
     innerMap1.physicsBody = [SKPhysicsBody bodyWithTexture:[SKTexture textureWithImageNamed:@"test"] size:CGSizeMake(innerMap1.frame.size.width*0.92, innerMap1.frame.size.height*0.92)]; 
     innerMap1.position = CGPointMake(-220, -140); 
     innerMap1.physicsBody.dynamic = NO; 

     [chapterSKSpriteNodes addObject:innerMap1]; 

这里是身体会变成什么样子skView.showsPhysics = YES;

enter image description here

所以我知道物理实体是我所需要的。但是,当我尝试从本机构创建GKObstacle时,在为了避免障碍目标而被分配时,它似乎不会成为障碍。

+0

物理实体不是免费的,您需要为您的节点创建它们。我在代码中没有看到这个代码 – Knight0fDragon

+0

对不起 - 有PB节点的TONS,所以我没有提供代码让我制作它们。我会举一个例子来更新我的问题 –

+0

也许你对事件的顺序有问题?你是否检查'obstacleNodePhysicsBodies'之前,看看数组中的节点是否有高度的身体? – Knight0fDragon

回答