這裏是我的問題:障礙隨着GameplayKit
- 我如何轉換的
SKSpriteNode
數組的GKObstacles
陣列,使得代理人可以適當避免這些障礙通過使用goalToAvoidObstacles:(nonnull NSArray<GKObstacle *> *) maxPredictionTime:(NSTimeInterval)
/?
看來,以何種方式,我創建了GKObstacle
陣列不允許GKGoal
正確識別這些的障礙,不管我給的目標是什麼重量。
我目前有幾個SKNode
s被添加到SKScene
chapterScene。這些節點中的每一個都被添加到我正在存儲的名爲障礙陣列的陣列中。我已成立了以下列方式工作得很好的測試:
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>"
)
其中每一種都被明確並適當地初始化,並添加到場景。這些元素中的每一個實際上都對SpriteKit
didBeginContact:(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;
:
所以我知道物理實體是我所需要的。但是,當我嘗試從本機構創建GKObstacle
時,在爲了避免障礙目標而被分配時,它似乎不會成爲障礙。
物理實體不是免費的,您需要爲您的節點創建它們。我在代碼中沒有看到這個代碼 – Knight0fDragon
對不起 - 有PB節點的TONS,所以我沒有提供代碼讓我製作它們。我會舉一個例子來更新我的問題 –
也許你對事件的順序有問題?你是否檢查'obstacleNodePhysicsBodies'之前,看看數組中的節點是否有高度的身體? – Knight0fDragon