与GameplayKit的障碍
这是我的问题:
- 如何将一个
SKSpriteNode
数组转换成一个SKSpriteNode
数组,以便代理人可以通过使用goalToAvoidObstacles:(nonnull NSArray<GKObstacle *> *) maxPredictionTime:(NSTimeInterval)
来适当地避免这些障碍goalToAvoidObstacles:(nonnull NSArray<GKObstacle *> *) maxPredictionTime:(NSTimeInterval)
/?
看来,我创buildGKObstacle
arrays的方式不允许GKGoal
正确地将这些障碍物识别为障碍物,无论我给目标带来多大的体重。
我目前有几个SKNode
被添加到SKScene
chapterScene 。 每个节点都被添加到我正在存储的数组中,名为obstacArray 。 我以下面的方式build立了一个很好的testing:
工作障碍
- (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
主体数组的行为。 我只能将这些手动创build的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>" )
每一个都已经清楚,适当地初始化并添加到现场。 这些元素中的每一个实际上都是与SpriteKit
didBeginContact:(SKPhysicsContact *)contact
方法didBeginContact:(SKPhysicsContact *)contact
,所以看起来节点正确地处理了它们应该具有的边界。
如果有人知道我在做什么错误或更好的方法把每个“障碍节点”转换成GKObstacle
障碍物,我会非常感激。 提前致谢!
更新 :这是一个与我正在testing的物理机构的节点:
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;
:
所以我知道物理体是我所需要的。 然而,当我尝试从这个机体创buildGKObstacle
障碍物时,在被指定为躲避障碍物目标时,它似乎没有成为障碍物。
这听起来像一个错误。 你应该检查来自obstacleNodePhysicsBodies的返回数组,确保你实际上回到你期望的障碍。 我怀疑你不是。
您可以检查障碍物上的相关顶点,如:
确保他们是你所期望的。