SpriteKit世界错误定位

时间:2016-04-07 12:15:53

标签: ios swift sprite-kit sknode

我正在尝试使用SpriteKit&迅速。我想我理解了世界SKNode的概念,但我不能让它正常工作。

我将world:SKNode作为孩子添加到GameScene。然后我添加9个子SKSpriteNodes(400px X 400px从200px X 200px点开始,因为spritesNodes居中)。而且他们定位完美,但我无法弄清楚如何获得我的世界SKNode的界限。

例如,如何展示我世界的左下角?

import SpriteKit

class GameScene: SKScene {
    let cityPartSide:Int = 400
    let startCoord:Int = 200

    var world:SKNode = SKNode()

    override func didMoveToView(view: SKView) {

        //self.world = SKNode()
        self.world.name = "world"
        self.addChild(self.world)
        let map:[[UInt8]] = [[16, 16, 16],
                             [16, 16, 16],
                             [16, 16, 16]]
        drawMap(world, map: map)

    }
    func drawMap(world:SKNode, map:[[UInt8]]) {
        let lineSize:Int = map[0].count
        var lineCounter = map.count
        while lineCounter > 0 {
            lineCounter -= 1
            var elemCounter = 0
            while elemCounter < lineSize {
                var sprite:SKSpriteNode = SKSpriteNode()
                let currentPartNumb = map[lineCounter][elemCounter]
                if currentPartNumb == 0 {
                    elemCounter += 1
                    continue
                } else {
                    sprite = SKSpriteNode(imageNamed: "cell")
                }
                //CHOOSE IMAGE
                sprite.position = CGPoint(x: startCoord + elemCounter * cityPartSide, y: startCoord + ((lineSize-1)-lineCounter)*cityPartSide)
                world.addChild(sprite)
                elemCounter += 1
            }
        }
    }
    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

但是我通过运行这段代码获得的是,它的垂直位置非常完美,但是世界的左侧是在屏幕外的某个位置。

enter image description here

0 个答案:

没有答案