更改SKNode.obstaclesFromNodeBounds中使用的坐标系?

时间:2015-11-02 22:41:24

标签: swift sprite-kit path-finding gamekit

我在游戏中设置寻路。如果我将所有障碍都放在自己身上,那么它就会很有效。

但是,如果我将障碍物放在一个名为“main”的容器(SKSpriteNode)中,那么它根本不起作用 - 我的玩家走到“尽头”完全无视障碍物。

我一直试图弄清楚它有一段时间,但看起来障碍仍然在我的场景坐标中,“自我”

所以我的问题是如何更改与SKNode.obstaclesFromNodeBounds一起使用的坐标系?我希望它相对于“main”的坐标系。

我的代码如下:

import SpriteKit
import GameplayKit

class GameScene: SKScene {
var player:SKSpriteNode!
override func didMoveToView(view: SKView) {

    let main = SKSpriteNode(color: UIColor.grayColor(), size: CGSize(width: 736, height: 414))
    main.position = CGPoint(x: 736/2, y: 414/2)
    self.addChild(main)

    player = SKSpriteNode(color: UIColor.blueColor(), size: CGSize(width: 50, height: 50))
    player.position = CGPoint(x: (main.frame.size.width-player.frame.size.width)/2, y: 0)
    main.addChild(player)

    let end = SKSpriteNode(color: UIColor.orangeColor(), size: CGSize(width: 50, height: 50))
    end.position = CGPoint(x: -(main.frame.size.width-player.frame.size.width)/2, y: 0)
    main.addChild(end)

    let obstacle = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 100))
    main.addChild(obstacle)

    let obstacleNodes = [obstacle]

    let obstacles = SKNode.obstaclesFromNodeBounds(obstacleNodes)

    let graph = GKObstacleGraph(obstacles: obstacles, bufferRadius: 30)

    for var i = 0; i < graph.obstacles.count; i++ {
        let obs = graph.obstacles[i]
        var string = "Count: \(obs.vertexCount)\n"
        for var k = 0; k < obs.vertexCount; k++ {
            let vertex = obs.vertexAtIndex(k)
            string += "Vertex \(k): \(vertex)\n"

        }
        print(string)
    }


    let startX = player.position.x
    let startY = player.position.y

    let endX = end.position.x
    let endY = end.position.y

    let startNode = GKGraphNode2D(point: float2(Float(startX), Float(startY)))
    let endNode = GKGraphNode2D(point: float2(Float(endX), Float(endY)))

    print(startNode)

    graph.connectNodeUsingObstacles(startNode)
    graph.connectNodeUsingObstacles(endNode)

    let path:[GKGraphNode] = graph.findPathFromNode(startNode, toNode: endNode)
    let cgPath = CGPathCreateMutable()
    print(path.count)

    CGPathMoveToPoint(cgPath, nil, CGFloat(startNode.position.x), CGFloat(startNode.position.y))
    for node:GKGraphNode in path {
        if let point2d = node as? GKGraphNode2D {
            let point = CGPoint(x: CGFloat(point2d.position.x), y: CGFloat(point2d.position.y))

            CGPathAddLineToPoint(cgPath, nil, point.x, point.y)
        }
    }

    let follow = SKAction.followPath(cgPath, asOffset: false, orientToPath: true, speed: 50)

    player.runAction(follow)
    player.paused = true
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    player.paused = false

}

override func update(currentTime: CFTimeInterval) {
}
}

障碍物的输出是:

Vertex 0: float2(343.0, 157.0)
Vertex 1: float2(393.0, 157.0)
Vertex 2: float2(393.0, 257.0)
Vertex 3: float2(343.0, 257.0)

但它应该是

-25, 50
25, 50
25, -50
-25, -50

感谢任何输入!

1 个答案:

答案 0 :(得分:1)

我无法找到任何方法来做到这一点,但我确实通过手动添加GKPolygonObstacles来解决这个问题:

 $this->db->join('members_profiles','members_profiles.mid = members.mid', 'left');

注意:此函数不能直接在自身中使用节点(因为它们具有不同的坐标系),但仅在另一个节点(如我所拥有的容器)中工作。